blob: c49e57c330ecbfda3c208496ebc35ac96fc29eae [file] [log] [blame]
reed@google.comdde09562011-05-23 12:21:05 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comdde09562011-05-23 12:21:05 +00006 */
7
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "SkReader32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
reed@google.comdde09562011-05-23 12:21:05 +000010
11static 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
18static 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
26static 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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000035DEF_TEST(Reader32, reporter) {
reed@google.comdde09562011-05-23 12:21:05 +000036 SkReader32 reader;
37 assert_empty(reporter, reader);
halcanary96fcdcc2015-08-27 07:41:13 -070038 REPORTER_ASSERT(reporter, nullptr == reader.base());
39 REPORTER_ASSERT(reporter, nullptr == reader.peek());
reed@google.comdde09562011-05-23 12:21:05 +000040
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 };
epoger@google.combf083a92011-06-08 18:26:08 +000045 const size_t bufsize = sizeof(data) > sizeof(data2) ?
46 sizeof(data) : sizeof(data2);
47 char buffer[bufsize];
reed@google.comdde09562011-05-23 12:21:05 +000048
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);
reed@google.com103c0362011-05-23 12:28:48 +000060 reader.read(buffer, sizeof(data));
61 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
reed@google.comdde09562011-05-23 12:21:05 +000062
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);
reed@google.com103c0362011-05-23 12:28:48 +000074 reader.read(buffer, sizeof(data2));
75 REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
reed@google.comdde09562011-05-23 12:21:05 +000076
halcanary96fcdcc2015-08-27 07:41:13 -070077 reader.setMemory(nullptr, 0);
reed@google.comdde09562011-05-23 12:21:05 +000078 assert_empty(reporter, reader);
halcanary96fcdcc2015-08-27 07:41:13 -070079 REPORTER_ASSERT(reporter, nullptr == reader.base());
80 REPORTER_ASSERT(reporter, nullptr == reader.peek());
reed@google.comdde09562011-05-23 12:21:05 +000081}