Reader32Test.cpp revision 0b15698a8c76bb8abc1b555c1d91892669b4118f
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 "Test.h"
20
21static void assert_eof(skiatest::Reporter* reporter, const SkReader32& reader) {
22    REPORTER_ASSERT(reporter, reader.eof());
23    REPORTER_ASSERT(reporter, reader.size() == reader.offset());
24    REPORTER_ASSERT(reporter, (const char*)reader.peek() ==
25                    (const char*)reader.base() + reader.size());
26}
27
28static void assert_start(skiatest::Reporter* reporter, const SkReader32& reader) {
29    REPORTER_ASSERT(reporter, 0 == reader.offset());
30    REPORTER_ASSERT(reporter, reader.size() == reader.available());
31    REPORTER_ASSERT(reporter, reader.isAvailable(reader.size()));
32    REPORTER_ASSERT(reporter, !reader.isAvailable(reader.size() + 1));
33    REPORTER_ASSERT(reporter, reader.peek() == reader.base());
34}
35
36static void assert_empty(skiatest::Reporter* reporter, const SkReader32& reader) {
37    REPORTER_ASSERT(reporter, 0 == reader.size());
38    REPORTER_ASSERT(reporter, 0 == reader.offset());
39    REPORTER_ASSERT(reporter, 0 == reader.available());
40    REPORTER_ASSERT(reporter, !reader.isAvailable(1));
41    assert_eof(reporter, reader);
42    assert_start(reporter, reader);
43}
44
45static void Tests(skiatest::Reporter* reporter) {
46    SkReader32 reader;
47    assert_empty(reporter, reader);
48    REPORTER_ASSERT(reporter, NULL == reader.base());
49    REPORTER_ASSERT(reporter, NULL == reader.peek());
50
51    size_t i;
52
53    const int32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
54    const SkScalar data2[] = { 0, SK_Scalar1, -SK_Scalar1, SK_Scalar1/2 };
55    char buffer[SkMax32(sizeof(data), sizeof(data2))];
56
57    reader.setMemory(data, sizeof(data));
58    for (i = 0; i < SK_ARRAY_COUNT(data); ++i) {
59        REPORTER_ASSERT(reporter, sizeof(data) == reader.size());
60        REPORTER_ASSERT(reporter, i*4 == reader.offset());
61        REPORTER_ASSERT(reporter, (const void*)data == reader.base());
62        REPORTER_ASSERT(reporter, (const void*)&data[i] == reader.peek());
63        REPORTER_ASSERT(reporter, data[i] == reader.readInt());
64    }
65    assert_eof(reporter, reader);
66    reader.rewind();
67    assert_start(reporter, reader);
68    reader.read(buffer, sizeof(data));
69    REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
70
71    reader.setMemory(data2, sizeof(data2));
72    for (i = 0; i < SK_ARRAY_COUNT(data2); ++i) {
73        REPORTER_ASSERT(reporter, sizeof(data2) == reader.size());
74        REPORTER_ASSERT(reporter, i*4 == reader.offset());
75        REPORTER_ASSERT(reporter, (const void*)data2 == reader.base());
76        REPORTER_ASSERT(reporter, (const void*)&data2[i] == reader.peek());
77        REPORTER_ASSERT(reporter, data2[i] == reader.readScalar());
78    }
79    assert_eof(reporter, reader);
80    reader.rewind();
81    assert_start(reporter, reader);
82    reader.read(buffer, sizeof(data2));
83    REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
84
85    reader.setMemory(NULL, 0);
86    assert_empty(reporter, reader);
87    REPORTER_ASSERT(reporter, NULL == reader.base());
88    REPORTER_ASSERT(reporter, NULL == reader.peek());
89}
90
91#include "TestClassDef.h"
92DEFINE_TESTCLASS("Reader32", Reader32Class, Tests)
93
94