reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 8 | #include "SkReader32.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 10 | |
| 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 | |
| 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 | |
| 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 35 | DEF_TEST(Reader32, reporter) { |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 36 | SkReader32 reader; |
| 37 | assert_empty(reporter, reader); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, nullptr == reader.base()); |
| 39 | REPORTER_ASSERT(reporter, nullptr == reader.peek()); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 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 }; |
epoger@google.com | bf083a9 | 2011-06-08 18:26:08 +0000 | [diff] [blame] | 45 | const size_t bufsize = sizeof(data) > sizeof(data2) ? |
| 46 | sizeof(data) : sizeof(data2); |
| 47 | char buffer[bufsize]; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 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); |
reed@google.com | 103c036 | 2011-05-23 12:28:48 +0000 | [diff] [blame] | 60 | reader.read(buffer, sizeof(data)); |
| 61 | REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data))); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 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); |
reed@google.com | 103c036 | 2011-05-23 12:28:48 +0000 | [diff] [blame] | 74 | reader.read(buffer, sizeof(data2)); |
| 75 | REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2))); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 76 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | reader.setMemory(nullptr, 0); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 78 | assert_empty(reporter, reader); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, nullptr == reader.base()); |
| 80 | REPORTER_ASSERT(reporter, nullptr == reader.peek()); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 81 | } |