Serenity Operating System
1/*
2 * Copyright (c) 2020-2021, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8
9#include <AK/Array.h>
10#include <LibCompress/Zlib.h>
11
12TEST_CASE(zlib_decompress_simple)
13{
14 Array<u8, 40> const compressed {
15 0x78, 0x01, 0x01, 0x1D, 0x00, 0xE2, 0xFF, 0x54, 0x68, 0x69, 0x73, 0x20,
16 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x69, 0x6D, 0x70, 0x6C, 0x65, 0x20,
17 0x74, 0x65, 0x78, 0x74, 0x20, 0x66, 0x69, 0x6C, 0x65, 0x20, 0x3A, 0x29,
18 0x99, 0x5E, 0x09, 0xE8
19 };
20
21 const u8 uncompressed[] = "This is a simple text file :)";
22
23 auto const decompressed = Compress::ZlibDecompressor::decompress_all(compressed);
24 EXPECT(decompressed.value().bytes() == (ReadonlyBytes { uncompressed, sizeof(uncompressed) - 1 }));
25}
26
27TEST_CASE(zlib_compress_simple)
28{
29 // Note: This is just the output of our compression function from an arbitrary point in time.
30 // This test is intended to ensure that the decompression doesn't change unintentionally,
31 // it does not make any guarantees for correctness.
32
33 Array<u8, 37> const compressed {
34 0x78, 0x9C, 0x0B, 0xC9, 0xC8, 0x2C, 0x56, 0xC8, 0x2C, 0x56, 0x48, 0x54,
35 0x28, 0xCE, 0xCC, 0x2D, 0xC8, 0x49, 0x55, 0x28, 0x49, 0xAD, 0x28, 0x51,
36 0x48, 0xCB, 0xCC, 0x49, 0x55, 0xB0, 0xD2, 0x04, 0x00, 0x99, 0x5E, 0x09,
37 0xE8
38 };
39
40 const u8 uncompressed[] = "This is a simple text file :)";
41
42 auto const freshly_pressed = Compress::ZlibCompressor::compress_all({ uncompressed, sizeof(uncompressed) - 1 });
43 EXPECT(freshly_pressed.value().bytes() == compressed.span());
44}