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