TestBrotli.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2022, Michiel Visser <opensource@webmichiel.nl>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <LibCompress/Brotli.h>
  8. #include <LibCore/Stream.h>
  9. static void run_test(StringView const file_name)
  10. {
  11. // This makes sure that the tests will run both on target and in Lagom.
  12. #ifdef AK_OS_SERENITY
  13. String path = String::formatted("/usr/Tests/LibCompress/brotli-test-files/{}", file_name);
  14. #else
  15. String path = String::formatted("brotli-test-files/{}", file_name);
  16. #endif
  17. auto cmp_file = MUST(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
  18. auto cmp_data = MUST(cmp_file->read_all());
  19. String path_compressed = String::formatted("{}.br", path);
  20. auto file = MUST(Core::Stream::File::open(path_compressed, Core::Stream::OpenMode::Read));
  21. auto brotli_stream = Compress::BrotliDecompressionStream { *file };
  22. auto data = MUST(brotli_stream.read_all());
  23. EXPECT_EQ(data, cmp_data);
  24. }
  25. TEST_CASE(brotli_decompress_uncompressed)
  26. {
  27. run_test("wellhello.txt"sv);
  28. }
  29. TEST_CASE(brotli_decompress_simple)
  30. {
  31. run_test("hello.txt"sv);
  32. }
  33. TEST_CASE(brotli_decompress_simple2)
  34. {
  35. run_test("wellhello2.txt"sv);
  36. }
  37. TEST_CASE(brotli_decompress_lorem)
  38. {
  39. run_test("lorem.txt"sv);
  40. }
  41. TEST_CASE(brotli_decompress_lorem2)
  42. {
  43. run_test("lorem2.txt"sv);
  44. }
  45. TEST_CASE(brotli_decompress_transform)
  46. {
  47. run_test("transform.txt"sv);
  48. }
  49. TEST_CASE(brotli_decompress_serenityos_html)
  50. {
  51. run_test("serenityos.html"sv);
  52. }
  53. TEST_CASE(brotli_decompress_happy3rd_html)
  54. {
  55. run_test("happy3rd.html"sv);
  56. }
  57. TEST_CASE(brotli_decompress_katica_regular_10_font)
  58. {
  59. run_test("KaticaRegular10.font"sv);
  60. }
  61. TEST_CASE(brotli_single_z)
  62. {
  63. run_test("single-z.txt"sv);
  64. }
  65. TEST_CASE(brotli_single_x)
  66. {
  67. run_test("single-x.txt"sv);
  68. }
  69. TEST_CASE(brotli_decompress_zero_one_bin)
  70. {
  71. // This makes sure that the tests will run both on target and in Lagom.
  72. #ifdef AK_OS_SERENITY
  73. String path = "/usr/Tests/LibCompress/brotli-test-files/zero-one.bin";
  74. #else
  75. String path = "brotli-test-files/zero-one.bin";
  76. #endif
  77. String path_compressed = String::formatted("{}.br", path);
  78. auto file = MUST(Core::Stream::File::open(path_compressed, Core::Stream::OpenMode::Read));
  79. auto brotli_stream = Compress::BrotliDecompressionStream { *file };
  80. u8 buffer_raw[4096];
  81. Bytes buffer { buffer_raw, 4096 };
  82. size_t bytes_read = 0;
  83. while (true) {
  84. size_t nread = MUST(brotli_stream.read(buffer)).size();
  85. if (nread == 0)
  86. break;
  87. for (size_t i = 0; i < nread; i++) {
  88. if (bytes_read < 16 * MiB)
  89. EXPECT(buffer[i] == 0);
  90. else
  91. EXPECT(buffer[i] == 1);
  92. }
  93. bytes_read += nread;
  94. }
  95. EXPECT(bytes_read == 32 * MiB);
  96. EXPECT(brotli_stream.is_eof());
  97. }