blob: d81287bd74f9bb06ae1bd8e5a6fc4813d809a628 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comdde09562011-05-23 12:21:05 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.comdde09562011-05-23 12:21:05 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comdde09562011-05-23 12:21:05 +000011#include "SkReader32.h"
12#include "Test.h"
13
14static void assert_eof(skiatest::Reporter* reporter, const SkReader32& reader) {
15 REPORTER_ASSERT(reporter, reader.eof());
16 REPORTER_ASSERT(reporter, reader.size() == reader.offset());
17 REPORTER_ASSERT(reporter, (const char*)reader.peek() ==
18 (const char*)reader.base() + reader.size());
19}
20
21static void assert_start(skiatest::Reporter* reporter, const SkReader32& reader) {
22 REPORTER_ASSERT(reporter, 0 == reader.offset());
23 REPORTER_ASSERT(reporter, reader.size() == reader.available());
24 REPORTER_ASSERT(reporter, reader.isAvailable(reader.size()));
25 REPORTER_ASSERT(reporter, !reader.isAvailable(reader.size() + 1));
26 REPORTER_ASSERT(reporter, reader.peek() == reader.base());
27}
28
29static void assert_empty(skiatest::Reporter* reporter, const SkReader32& reader) {
30 REPORTER_ASSERT(reporter, 0 == reader.size());
31 REPORTER_ASSERT(reporter, 0 == reader.offset());
32 REPORTER_ASSERT(reporter, 0 == reader.available());
33 REPORTER_ASSERT(reporter, !reader.isAvailable(1));
34 assert_eof(reporter, reader);
35 assert_start(reporter, reader);
36}
37
38static void Tests(skiatest::Reporter* reporter) {
39 SkReader32 reader;
40 assert_empty(reporter, reader);
41 REPORTER_ASSERT(reporter, NULL == reader.base());
42 REPORTER_ASSERT(reporter, NULL == reader.peek());
43
44 size_t i;
45
46 const int32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
47 const SkScalar data2[] = { 0, SK_Scalar1, -SK_Scalar1, SK_Scalar1/2 };
epoger@google.combf083a92011-06-08 18:26:08 +000048 const size_t bufsize = sizeof(data) > sizeof(data2) ?
49 sizeof(data) : sizeof(data2);
50 char buffer[bufsize];
reed@google.comdde09562011-05-23 12:21:05 +000051
52 reader.setMemory(data, sizeof(data));
53 for (i = 0; i < SK_ARRAY_COUNT(data); ++i) {
54 REPORTER_ASSERT(reporter, sizeof(data) == reader.size());
55 REPORTER_ASSERT(reporter, i*4 == reader.offset());
56 REPORTER_ASSERT(reporter, (const void*)data == reader.base());
57 REPORTER_ASSERT(reporter, (const void*)&data[i] == reader.peek());
58 REPORTER_ASSERT(reporter, data[i] == reader.readInt());
59 }
60 assert_eof(reporter, reader);
61 reader.rewind();
62 assert_start(reporter, reader);
reed@google.com103c0362011-05-23 12:28:48 +000063 reader.read(buffer, sizeof(data));
64 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
reed@google.comdde09562011-05-23 12:21:05 +000065
66 reader.setMemory(data2, sizeof(data2));
67 for (i = 0; i < SK_ARRAY_COUNT(data2); ++i) {
68 REPORTER_ASSERT(reporter, sizeof(data2) == reader.size());
69 REPORTER_ASSERT(reporter, i*4 == reader.offset());
70 REPORTER_ASSERT(reporter, (const void*)data2 == reader.base());
71 REPORTER_ASSERT(reporter, (const void*)&data2[i] == reader.peek());
72 REPORTER_ASSERT(reporter, data2[i] == reader.readScalar());
73 }
74 assert_eof(reporter, reader);
75 reader.rewind();
76 assert_start(reporter, reader);
reed@google.com103c0362011-05-23 12:28:48 +000077 reader.read(buffer, sizeof(data2));
78 REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
reed@google.comdde09562011-05-23 12:21:05 +000079
80 reader.setMemory(NULL, 0);
81 assert_empty(reporter, reader);
82 REPORTER_ASSERT(reporter, NULL == reader.base());
83 REPORTER_ASSERT(reporter, NULL == reader.peek());
84}
85
86#include "TestClassDef.h"
87DEFINE_TESTCLASS("Reader32", Reader32Class, Tests)