• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 
11 #include "SkReader32.h"
12 #include "SkWriter32.h"
13 #include "Test.h"
14 
test1(skiatest::Reporter * reporter,SkWriter32 * writer)15 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
16     const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
17     for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
18         REPORTER_ASSERT(reporter, i*4 == writer->size());
19         writer->write32(data[i]);
20         uint32_t* addr = writer->peek32(i * 4);
21         REPORTER_ASSERT(reporter, data[i] == *addr);
22     }
23 
24     char buffer[sizeof(data)];
25     REPORTER_ASSERT(reporter, sizeof(buffer) == writer->size());
26     writer->flatten(buffer);
27     REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
28 }
29 
test2(skiatest::Reporter * reporter,SkWriter32 * writer)30 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
31     static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
32     size_t i;
33 
34     size_t len = 0;
35     for (i = 0; i <= 26; ++i) {
36         len += SkWriter32::WriteStringSize(gStr, i);
37         writer->writeString(gStr, i);
38     }
39     REPORTER_ASSERT(reporter, writer->size() == len);
40 
41     SkAutoMalloc storage(len);
42     writer->flatten(storage.get());
43 
44     SkReader32 reader;
45     reader.setMemory(storage.get(), len);
46     for (i = 0; i <= 26; ++i) {
47         REPORTER_ASSERT(reporter, !reader.eof());
48         const char* str = reader.readString(&len);
49         REPORTER_ASSERT(reporter, i == len);
50         REPORTER_ASSERT(reporter, strlen(str) == len);
51         REPORTER_ASSERT(reporter, !memcmp(str, gStr, len));
52     }
53     REPORTER_ASSERT(reporter, reader.eof());
54 }
55 
Tests(skiatest::Reporter * reporter)56 static void Tests(skiatest::Reporter* reporter) {
57     // dynamic allocator
58     {
59         SkWriter32 writer(256 * 4);
60         REPORTER_ASSERT(reporter, NULL == writer.getSingleBlock());
61         test1(reporter, &writer);
62 
63         writer.reset();
64         test2(reporter, &writer);
65     }
66 
67     // single-block
68     {
69         SkWriter32 writer(0);
70         uint32_t storage[256];
71         REPORTER_ASSERT(reporter, NULL == writer.getSingleBlock());
72         writer.reset(storage, sizeof(storage));
73         REPORTER_ASSERT(reporter, (void*)storage == writer.getSingleBlock());
74         test1(reporter, &writer);
75 
76         writer.reset(storage, sizeof(storage));
77         test2(reporter, &writer);
78     }
79 }
80 
81 #include "TestClassDef.h"
82 DEFINE_TESTCLASS("Writer32", Writer32Class, Tests)
83 
84