Home | History | Annotate | Download | only in tests
      1 /*
      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.
      6  */
      7 
      8 #include "Test.h"
      9 #include "TestClassDef.h"
     10 #include "SkData.h"
     11 #include "SkDataTable.h"
     12 #include "SkOrderedReadBuffer.h"
     13 #include "SkOrderedWriteBuffer.h"
     14 #include "SkOSFile.h"
     15 #include "SkStream.h"
     16 
     17 static void test_is_equal(skiatest::Reporter* reporter,
     18                           const SkDataTable* a, const SkDataTable* b) {
     19     REPORTER_ASSERT(reporter, a->count() == b->count());
     20     for (int i = 0; i < a->count(); ++i) {
     21         size_t sizea, sizeb;
     22         const void* mema = a->at(i, &sizea);
     23         const void* memb = b->at(i, &sizeb);
     24         REPORTER_ASSERT(reporter, sizea == sizeb);
     25         REPORTER_ASSERT(reporter, !memcmp(mema, memb, sizea));
     26     }
     27 }
     28 
     29 static void test_datatable_is_empty(skiatest::Reporter* reporter,
     30                                     SkDataTable* table) {
     31     REPORTER_ASSERT(reporter, table->isEmpty());
     32     REPORTER_ASSERT(reporter, 0 == table->count());
     33 }
     34 
     35 static void test_emptytable(skiatest::Reporter* reporter) {
     36     SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty());
     37     SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(NULL, NULL, 0));
     38     SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(NULL, 0, 0));
     39     SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(NULL, 0, 0,
     40                                                                NULL, NULL));
     41 
     42     test_datatable_is_empty(reporter, table0);
     43     test_datatable_is_empty(reporter, table1);
     44     test_datatable_is_empty(reporter, table2);
     45     test_datatable_is_empty(reporter, table3);
     46 
     47     test_is_equal(reporter, table0, table1);
     48     test_is_equal(reporter, table0, table2);
     49     test_is_equal(reporter, table0, table3);
     50 }
     51 
     52 static void test_simpletable(skiatest::Reporter* reporter) {
     53     const int idata[] = { 1, 4, 9, 16, 25, 63 };
     54     int icount = SK_ARRAY_COUNT(idata);
     55     SkAutoTUnref<SkDataTable> itable(SkDataTable::NewCopyArray(idata,
     56                                                                sizeof(idata[0]),
     57                                                                icount));
     58     REPORTER_ASSERT(reporter, itable->count() == icount);
     59     for (int i = 0; i < icount; ++i) {
     60         size_t size;
     61         REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i));
     62         REPORTER_ASSERT(reporter, *itable->atT<int>(i, &size) == idata[i]);
     63         REPORTER_ASSERT(reporter, sizeof(int) == size);
     64     }
     65 }
     66 
     67 static void test_vartable(skiatest::Reporter* reporter) {
     68     const char* str[] = {
     69         "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
     70     };
     71     int count = SK_ARRAY_COUNT(str);
     72     size_t sizes[SK_ARRAY_COUNT(str)];
     73     for (int i = 0; i < count; ++i) {
     74         sizes[i] = strlen(str[i]) + 1;
     75     }
     76 
     77     SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays(
     78                                         (const void*const*)str, sizes, count));
     79 
     80     REPORTER_ASSERT(reporter, table->count() == count);
     81     for (int i = 0; i < count; ++i) {
     82         size_t size;
     83         REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]);
     84         REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
     85                                           str[i]));
     86         REPORTER_ASSERT(reporter, size == sizes[i]);
     87 
     88         const char* s = table->atStr(i);
     89         REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
     90     }
     91 }
     92 
     93 static void test_tablebuilder(skiatest::Reporter* reporter) {
     94     const char* str[] = {
     95         "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg"
     96     };
     97     int count = SK_ARRAY_COUNT(str);
     98 
     99     SkDataTableBuilder builder(16);
    100 
    101     for (int i = 0; i < count; ++i) {
    102         builder.append(str[i], strlen(str[i]) + 1);
    103     }
    104     SkAutoTUnref<SkDataTable> table(builder.detachDataTable());
    105 
    106     REPORTER_ASSERT(reporter, table->count() == count);
    107     for (int i = 0; i < count; ++i) {
    108         size_t size;
    109         REPORTER_ASSERT(reporter, table->atSize(i) == strlen(str[i]) + 1);
    110         REPORTER_ASSERT(reporter, !strcmp(table->atT<const char>(i, &size),
    111                                           str[i]));
    112         REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1);
    113 
    114         const char* s = table->atStr(i);
    115         REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i]));
    116     }
    117 }
    118 
    119 static void test_globaltable(skiatest::Reporter* reporter) {
    120     static const int gData[] = {
    121         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
    122     };
    123     int count = SK_ARRAY_COUNT(gData);
    124 
    125     SkAutoTUnref<SkDataTable> table(SkDataTable::NewArrayProc(gData,
    126                                           sizeof(gData[0]), count, NULL, NULL));
    127 
    128     REPORTER_ASSERT(reporter, table->count() == count);
    129     for (int i = 0; i < count; ++i) {
    130         size_t size;
    131         REPORTER_ASSERT(reporter, table->atSize(i) == sizeof(int));
    132         REPORTER_ASSERT(reporter, *table->atT<const char>(i, &size) == i);
    133         REPORTER_ASSERT(reporter, sizeof(int) == size);
    134     }
    135 }
    136 
    137 DEF_TEST(DataTable, reporter) {
    138     test_emptytable(reporter);
    139     test_simpletable(reporter);
    140     test_vartable(reporter);
    141     test_tablebuilder(reporter);
    142     test_globaltable(reporter);
    143 }
    144 
    145 static void* gGlobal;
    146 
    147 static void delete_int_proc(const void* ptr, size_t len, void* context) {
    148     int* data = (int*)ptr;
    149     SkASSERT(context == gGlobal);
    150     delete[] data;
    151 }
    152 
    153 static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
    154     REPORTER_ASSERT(reporter, ref->size() == len);
    155 }
    156 
    157 static void assert_data(skiatest::Reporter* reporter, SkData* ref,
    158                         const void* data, size_t len) {
    159     REPORTER_ASSERT(reporter, ref->size() == len);
    160     REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
    161 }
    162 
    163 static void test_cstring(skiatest::Reporter* reporter) {
    164     const char str[] = "Hello world";
    165     size_t     len = strlen(str);
    166 
    167     SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
    168     SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
    169 
    170     REPORTER_ASSERT(reporter, r0->equals(r1));
    171 
    172     SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
    173     REPORTER_ASSERT(reporter, 1 == r2->size());
    174     REPORTER_ASSERT(reporter, 0 == *r2->bytes());
    175 }
    176 
    177 static void test_files(skiatest::Reporter* reporter) {
    178     SkString tmpDir = skiatest::Test::GetTmpDir();
    179     if (tmpDir.isEmpty()) {
    180         return;
    181     }
    182 
    183     SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "data_test");
    184 
    185     const char s[] = "abcdefghijklmnopqrstuvwxyz";
    186     {
    187         SkFILEWStream writer(path.c_str());
    188         if (!writer.isValid()) {
    189             SkString msg;
    190             msg.printf("Failed to create tmp file %s\n", path.c_str());
    191             reporter->reportFailed(msg);
    192             return;
    193         }
    194         writer.write(s, 26);
    195     }
    196 
    197     SkFILE* file = sk_fopen(path.c_str(), kRead_SkFILE_Flag);
    198     SkAutoTUnref<SkData> r1(SkData::NewFromFILE(file));
    199     REPORTER_ASSERT(reporter, r1.get() != NULL);
    200     REPORTER_ASSERT(reporter, r1->size() == 26);
    201     REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r1->data()), s, 26) == 0);
    202 
    203     int fd = sk_fileno(file);
    204     SkAutoTUnref<SkData> r2(SkData::NewFromFD(fd));
    205     REPORTER_ASSERT(reporter, r2.get() != NULL);
    206     REPORTER_ASSERT(reporter, r2->size() == 26);
    207     REPORTER_ASSERT(reporter, strncmp(static_cast<const char*>(r2->data()), s, 26) == 0);
    208 }
    209 
    210 DEF_TEST(Data, reporter) {
    211     const char* str = "We the people, in order to form a more perfect union.";
    212     const int N = 10;
    213 
    214     SkAutoTUnref<SkData> r0(SkData::NewEmpty());
    215     SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
    216     SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
    217                                            delete_int_proc, gGlobal));
    218     SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
    219 
    220     assert_len(reporter, r0, 0);
    221     assert_len(reporter, r1, strlen(str));
    222     assert_len(reporter, r2, N * sizeof(int));
    223     assert_len(reporter, r3, 6);
    224 
    225     assert_data(reporter, r1, str, strlen(str));
    226     assert_data(reporter, r3, "people", 6);
    227 
    228     SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
    229     assert_len(reporter, tmp, 0);
    230     tmp->unref();
    231     tmp = SkData::NewSubset(r1, 0, 0);
    232     assert_len(reporter, tmp, 0);
    233     tmp->unref();
    234 
    235     test_cstring(reporter);
    236     test_files(reporter);
    237 }
    238