• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright 2011 Google Inc.
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8          http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16 
17 
18 #include "SkReader32.h"
19 #include "SkWriter32.h"
20 #include "Test.h"
21 
test1(skiatest::Reporter * reporter,SkWriter32 * writer)22 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
23     const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
24     for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
25         REPORTER_ASSERT(reporter, i*4 == writer->size());
26         writer->write32(data[i]);
27         uint32_t* addr = writer->peek32(i * 4);
28         REPORTER_ASSERT(reporter, data[i] == *addr);
29     }
30 
31     char buffer[sizeof(data)];
32     REPORTER_ASSERT(reporter, sizeof(buffer) == writer->size());
33     writer->flatten(buffer);
34     REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
35 }
36 
test2(skiatest::Reporter * reporter,SkWriter32 * writer)37 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
38     static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
39     size_t i;
40 
41     size_t len = 0;
42     for (i = 0; i <= 26; ++i) {
43         len += SkWriter32::WriteStringSize(gStr, i);
44         writer->writeString(gStr, i);
45     }
46     REPORTER_ASSERT(reporter, writer->size() == len);
47 
48     SkAutoMalloc storage(len);
49     writer->flatten(storage.get());
50 
51     SkReader32 reader;
52     reader.setMemory(storage.get(), len);
53     for (i = 0; i <= 26; ++i) {
54         REPORTER_ASSERT(reporter, !reader.eof());
55         const char* str = reader.readString(&len);
56         REPORTER_ASSERT(reporter, i == len);
57         REPORTER_ASSERT(reporter, strlen(str) == len);
58         REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
59     }
60     REPORTER_ASSERT(reporter, reader.eof());
61 }
62 
Tests(skiatest::Reporter * reporter)63 static void Tests(skiatest::Reporter* reporter) {
64     // dynamic allocator
65     {
66         SkWriter32 writer(256 * 4);
67         REPORTER_ASSERT(reporter, NULL == writer.getSingleBlock());
68         test1(reporter, &writer);
69 
70         writer.reset();
71         test2(reporter, &writer);
72     }
73 
74     // single-block
75     {
76         SkWriter32 writer(0);
77         uint32_t storage[256];
78         REPORTER_ASSERT(reporter, NULL == writer.getSingleBlock());
79         writer.reset(storage, sizeof(storage));
80         REPORTER_ASSERT(reporter, (void*)storage == writer.getSingleBlock());
81         test1(reporter, &writer);
82 
83         writer.reset(storage, sizeof(storage));
84         test2(reporter, &writer);
85     }
86 }
87 
88 #include "TestClassDef.h"
89 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests)
90 
91