Home | History | Annotate | Download | only in tests
      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 
     21 static 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 
     28 static 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 
     36 static 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 
     45 static 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     const size_t bufsize = sizeof(data) > sizeof(data2) ?
     56       sizeof(data) : sizeof(data2);
     57     char buffer[bufsize];
     58 
     59     reader.setMemory(data, sizeof(data));
     60     for (i = 0; i < SK_ARRAY_COUNT(data); ++i) {
     61         REPORTER_ASSERT(reporter, sizeof(data) == reader.size());
     62         REPORTER_ASSERT(reporter, i*4 == reader.offset());
     63         REPORTER_ASSERT(reporter, (const void*)data == reader.base());
     64         REPORTER_ASSERT(reporter, (const void*)&data[i] == reader.peek());
     65         REPORTER_ASSERT(reporter, data[i] == reader.readInt());
     66     }
     67     assert_eof(reporter, reader);
     68     reader.rewind();
     69     assert_start(reporter, reader);
     70     reader.read(buffer, sizeof(data));
     71     REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(data)));
     72 
     73     reader.setMemory(data2, sizeof(data2));
     74     for (i = 0; i < SK_ARRAY_COUNT(data2); ++i) {
     75         REPORTER_ASSERT(reporter, sizeof(data2) == reader.size());
     76         REPORTER_ASSERT(reporter, i*4 == reader.offset());
     77         REPORTER_ASSERT(reporter, (const void*)data2 == reader.base());
     78         REPORTER_ASSERT(reporter, (const void*)&data2[i] == reader.peek());
     79         REPORTER_ASSERT(reporter, data2[i] == reader.readScalar());
     80     }
     81     assert_eof(reporter, reader);
     82     reader.rewind();
     83     assert_start(reporter, reader);
     84     reader.read(buffer, sizeof(data2));
     85     REPORTER_ASSERT(reporter, !memcmp(data2, buffer, sizeof(data2)));
     86 
     87     reader.setMemory(NULL, 0);
     88     assert_empty(reporter, reader);
     89     REPORTER_ASSERT(reporter, NULL == reader.base());
     90     REPORTER_ASSERT(reporter, NULL == reader.peek());
     91 }
     92 
     93 #include "TestClassDef.h"
     94 DEFINE_TESTCLASS("Reader32", Reader32Class, Tests)
     95 
     96