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 "SkChunkAlloc.h"
     10 #include "SkUtils.h"
     11 
     12 static void test_chunkalloc(skiatest::Reporter* reporter) {
     13     size_t min = 256;
     14     SkChunkAlloc alloc(min);
     15 
     16     REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
     17     REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
     18     REPORTER_ASSERT(reporter, !alloc.contains(NULL));
     19     REPORTER_ASSERT(reporter, !alloc.contains(reporter));
     20 
     21     alloc.reset();
     22     REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
     23     REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
     24 
     25     size_t size = min >> 1;
     26     void* ptr = alloc.allocThrow(size);
     27     REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size);
     28     REPORTER_ASSERT(reporter, alloc.blockCount() > 0);
     29     REPORTER_ASSERT(reporter, alloc.contains(ptr));
     30 
     31     alloc.reset();
     32     REPORTER_ASSERT(reporter, !alloc.contains(ptr));
     33 }
     34 
     35 ///////////////////////////////////////////////////////////////////////////////
     36 
     37 static void set_zero(void* dst, size_t bytes) {
     38     char* ptr = (char*)dst;
     39     for (size_t i = 0; i < bytes; ++i) {
     40         ptr[i] = 0;
     41     }
     42 }
     43 
     44 #define MAX_ALIGNMENT   64
     45 #define MAX_COUNT       ((MAX_ALIGNMENT) * 32)
     46 #define PAD             32
     47 #define TOTAL           (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
     48 
     49 #define VALUE16         0x1234
     50 #define VALUE32         0x12345678
     51 
     52 static bool compare16(const uint16_t base[], uint16_t value, int count) {
     53     for (int i = 0; i < count; ++i) {
     54         if (base[i] != value) {
     55             SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
     56             return false;
     57         }
     58     }
     59     return true;
     60 }
     61 
     62 static bool compare32(const uint32_t base[], uint32_t value, int count) {
     63     for (int i = 0; i < count; ++i) {
     64         if (base[i] != value) {
     65             SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
     66             return false;
     67         }
     68     }
     69     return true;
     70 }
     71 
     72 static void test_16(skiatest::Reporter* reporter) {
     73     uint16_t buffer[TOTAL];
     74 
     75     for (int count = 0; count < MAX_COUNT; ++count) {
     76         for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
     77             set_zero(buffer, sizeof(buffer));
     78 
     79             uint16_t* base = &buffer[PAD + alignment];
     80             sk_memset16(base, VALUE16, count);
     81 
     82             compare16(buffer,       0,       PAD + alignment);
     83             compare16(base,         VALUE16, count);
     84             compare16(base + count, 0,       TOTAL - count - PAD - alignment);
     85         }
     86     }
     87 }
     88 
     89 static void test_32(skiatest::Reporter* reporter) {
     90     uint32_t buffer[TOTAL];
     91 
     92     for (int count = 0; count < MAX_COUNT; ++count) {
     93         for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
     94             set_zero(buffer, sizeof(buffer));
     95 
     96             uint32_t* base = &buffer[PAD + alignment];
     97             sk_memset32(base, VALUE32, count);
     98 
     99             compare32(buffer,       0,       PAD + alignment);
    100             compare32(base,         VALUE32, count);
    101             compare32(base + count, 0,       TOTAL - count - PAD - alignment);
    102         }
    103     }
    104 }
    105 
    106 /**
    107  *  Test sk_memset16 and sk_memset32.
    108  *  For performance considerations, implementations may take different paths
    109  *  depending on the alignment of the dst, and/or the size of the count.
    110  */
    111 static void TestMemset(skiatest::Reporter* reporter) {
    112     test_16(reporter);
    113     test_32(reporter);
    114 
    115     test_chunkalloc(reporter);
    116 };
    117 
    118 #include "TestClassDef.h"
    119 DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset)
    120