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