• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkReader32.h"
9 #include "Test.h"
10 
assert_eof(skiatest::Reporter * reporter,const SkReader32 & reader)11 static void assert_eof(skiatest::Reporter* reporter, const SkReader32& reader) {
12     REPORTER_ASSERT(reporter, reader.eof());
13     REPORTER_ASSERT(reporter, reader.size() == reader.offset());
14     REPORTER_ASSERT(reporter, (const char*)reader.peek() ==
15                     (const char*)reader.base() + reader.size());
16 }
17 
assert_start(skiatest::Reporter * reporter,const SkReader32 & reader)18 static void assert_start(skiatest::Reporter* reporter, const SkReader32& reader) {
19     REPORTER_ASSERT(reporter, 0 == reader.offset());
20     REPORTER_ASSERT(reporter, reader.size() == reader.available());
21     REPORTER_ASSERT(reporter, reader.isAvailable(reader.size()));
22     REPORTER_ASSERT(reporter, !reader.isAvailable(reader.size() + 1));
23     REPORTER_ASSERT(reporter, reader.peek() == reader.base());
24 }
25 
assert_empty(skiatest::Reporter * reporter,const SkReader32 & reader)26 static void assert_empty(skiatest::Reporter* reporter, const SkReader32& reader) {
27     REPORTER_ASSERT(reporter, 0 == reader.size());
28     REPORTER_ASSERT(reporter, 0 == reader.offset());
29     REPORTER_ASSERT(reporter, 0 == reader.available());
30     REPORTER_ASSERT(reporter, !reader.isAvailable(1));
31     assert_eof(reporter, reader);
32     assert_start(reporter, reader);
33 }
34 
DEF_TEST(Reader32,reporter)35 DEF_TEST(Reader32, reporter) {
36     SkReader32 reader;
37     assert_empty(reporter, reader);
38     REPORTER_ASSERT(reporter, nullptr == reader.base());
39     REPORTER_ASSERT(reporter, nullptr == reader.peek());
40 
41     size_t i;
42 
43     const int32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
44     const SkScalar data2[] = { 0, SK_Scalar1, -SK_Scalar1, SK_Scalar1/2 };
45     const size_t bufsize = sizeof(data) > sizeof(data2) ?
46       sizeof(data) : sizeof(data2);
47     char buffer[bufsize];
48 
49     reader.setMemory(data, sizeof(data));
50     for (i = 0; i < SK_ARRAY_COUNT(data); ++i) {
51         REPORTER_ASSERT(reporter, sizeof(data) == reader.size());
52         REPORTER_ASSERT(reporter, i*4 == reader.offset());
53         REPORTER_ASSERT(reporter, (const void*)data == reader.base());
54         REPORTER_ASSERT(reporter, (const void*)&data[i] == reader.peek());
55         REPORTER_ASSERT(reporter, data[i] == reader.readInt());
56     }
57     assert_eof(reporter, reader);
58     reader.rewind();
59     assert_start(reporter, reader);
60     reader.read(buffer, sizeof(data));
61     REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
62 
63     reader.setMemory(data2, sizeof(data2));
64     for (i = 0; i < SK_ARRAY_COUNT(data2); ++i) {
65         REPORTER_ASSERT(reporter, sizeof(data2) == reader.size());
66         REPORTER_ASSERT(reporter, i*4 == reader.offset());
67         REPORTER_ASSERT(reporter, (const void*)data2 == reader.base());
68         REPORTER_ASSERT(reporter, (const void*)&data2[i] == reader.peek());
69         REPORTER_ASSERT(reporter, data2[i] == reader.readScalar());
70     }
71     assert_eof(reporter, reader);
72     reader.rewind();
73     assert_start(reporter, reader);
74     reader.read(buffer, sizeof(data2));
75     REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
76 
77     reader.setMemory(nullptr, 0);
78     assert_empty(reporter, reader);
79     REPORTER_ASSERT(reporter, nullptr == reader.base());
80     REPORTER_ASSERT(reporter, nullptr == reader.peek());
81 
82     // need to handle read(null, 0) and not get undefined behavior from memcpy
83     {
84         char storage[100];
85         reader.setMemory(storage, sizeof(storage));
86         char buffer[10];
87         reader.read(buffer, 0);     // easy case, since we pass a ptr
88         reader.read(nullptr, 0);    // undef case, read() can't blindly call memcpy
89     }
90 }
91