halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 8 | #include "SkFlate.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "Test.h" |
| 11 | |
| 12 | #ifndef SK_NO_FLATE |
| 13 | |
| 14 | DEF_TEST(SkDeflateWStream, r) { |
| 15 | SkRandom random(123456); |
| 16 | for (int i = 0; i < 50; ++i) { |
| 17 | uint32_t size = random.nextULessThan(10000); |
| 18 | SkAutoTMalloc<uint8_t> buffer(size); |
mtklein | e6cf9cb | 2015-02-26 13:25:05 -0800 | [diff] [blame] | 19 | for (uint32_t j = 0; j < size; ++j) { |
| 20 | buffer[j] = random.nextU() & 0xff; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | SkDynamicMemoryWStream dynamicMemoryWStream; |
| 24 | { |
| 25 | SkDeflateWStream deflateWStream(&dynamicMemoryWStream); |
| 26 | uint32_t j = 0; |
| 27 | while (j < size) { |
| 28 | uint32_t writeSize = |
| 29 | SkTMin(size - j, random.nextRangeU(1, 400)); |
| 30 | if (!deflateWStream.write(&buffer[j], writeSize)) { |
| 31 | ERRORF(r, "something went wrong."); |
| 32 | return; |
| 33 | } |
| 34 | j += writeSize; |
| 35 | } |
| 36 | } |
| 37 | SkAutoTDelete<SkStreamAsset> compressed( |
| 38 | dynamicMemoryWStream.detachAsStream()); |
| 39 | |
| 40 | SkDynamicMemoryWStream decompressedDynamicMemoryWStream; |
| 41 | SkAssertResult(SkFlate::Inflate(compressed, |
| 42 | &decompressedDynamicMemoryWStream)); |
| 43 | |
| 44 | SkAutoTDelete<SkStreamAsset> decompressed( |
| 45 | decompressedDynamicMemoryWStream.detachAsStream()); |
| 46 | |
| 47 | if (decompressed->getLength() != size) { |
| 48 | ERRORF(r, "Decompression failed to get right size [%d]." |
| 49 | " %u != %u", i, (unsigned)(decompressed->getLength()), |
| 50 | (unsigned)size); |
| 51 | SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); |
| 52 | SkFILEWStream o(s.c_str()); |
| 53 | o.writeStream(compressed.get(), compressed->getLength()); |
| 54 | compressed->rewind(); |
| 55 | |
| 56 | s = SkStringPrintf("/tmp/deftst_input_%d", i); |
| 57 | SkFILEWStream o2(s.c_str()); |
| 58 | o2.write(&buffer[0], size); |
| 59 | |
| 60 | continue; |
| 61 | } |
| 62 | uint32_t minLength = SkTMin(size, |
| 63 | (uint32_t)(decompressed->getLength())); |
| 64 | for (uint32_t i = 0; i < minLength; ++i) { |
| 65 | uint8_t c; |
| 66 | SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); |
| 67 | SkASSERT(sizeof(uint8_t) == rb); |
| 68 | if (buffer[i] != c) { |
| 69 | ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | #endif // SK_NO_FLATE |