epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 11 | #include "SkReader32.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | static 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 | |
| 21 | static 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 | |
| 29 | static 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 | |
| 38 | static 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.com | bf083a9 | 2011-06-08 18:26:08 +0000 | [diff] [blame] | 48 | const size_t bufsize = sizeof(data) > sizeof(data2) ? |
| 49 | sizeof(data) : sizeof(data2); |
| 50 | char buffer[bufsize]; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 51 | |
| 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.com | 103c036 | 2011-05-23 12:28:48 +0000 | [diff] [blame] | 63 | reader.read(buffer, sizeof(data)); |
| 64 | REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data))); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 65 | |
| 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.com | 103c036 | 2011-05-23 12:28:48 +0000 | [diff] [blame] | 77 | reader.read(buffer, sizeof(data2)); |
| 78 | REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2))); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 79 | |
| 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" |
| 87 | DEFINE_TESTCLASS("Reader32", Reader32Class, Tests) |
| 88 | |