mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 7 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 9 | #include "SkChunkAlloc.h" |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 10 | #include "SkUtils.h" |
| 11 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 12 | static void test_chunkalloc(skiatest::Reporter* reporter) { |
| 13 | size_t min = 256; |
| 14 | SkChunkAlloc alloc(min); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 15 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 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)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 31 | alloc.reset(); |
| 32 | REPORTER_ASSERT(reporter, !alloc.contains(ptr)); |
| 33 | } |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 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]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 75 | for (int count = 0; count < MAX_COUNT; ++count) { |
| 76 | for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) { |
| 77 | set_zero(buffer, sizeof(buffer)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 79 | uint16_t* base = &buffer[PAD + alignment]; |
| 80 | sk_memset16(base, VALUE16, count); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, |
| 83 | compare16(buffer, 0, PAD + alignment) && |
| 84 | compare16(base, VALUE16, count) && |
| 85 | compare16(base + count, 0, TOTAL - count - PAD - alignment)); |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void test_32(skiatest::Reporter* reporter) { |
| 91 | uint32_t buffer[TOTAL]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 92 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 93 | for (int count = 0; count < MAX_COUNT; ++count) { |
| 94 | for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) { |
| 95 | set_zero(buffer, sizeof(buffer)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 96 | |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 97 | uint32_t* base = &buffer[PAD + alignment]; |
| 98 | sk_memset32(base, VALUE32, count); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, |
| 101 | compare32(buffer, 0, PAD + alignment) && |
| 102 | compare32(base, VALUE32, count) && |
| 103 | compare32(base + count, 0, TOTAL - count - PAD - alignment)); |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Test sk_memset16 and sk_memset32. |
| 110 | * For performance considerations, implementations may take different paths |
| 111 | * depending on the alignment of the dst, and/or the size of the count. |
| 112 | */ |
| 113 | static void TestMemset(skiatest::Reporter* reporter) { |
| 114 | test_16(reporter); |
| 115 | test_32(reporter); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 116 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 117 | test_chunkalloc(reporter); |
mike@reedtribe.org | c52b192 | 2012-01-07 03:49:13 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | #include "TestClassDef.h" |
| 121 | DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset) |