1 //===-- sanitizer_lzw_test.cpp ----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 #include "sanitizer_common/sanitizer_lzw.h"
13 #include "gtest/gtest.h"
14 #include "sanitizer_hash.h"
16 namespace __sanitizer
{
19 struct LzwTest
: public ::testing::Test
{
20 template <typename Generator
>
21 void Run(size_t n
, Generator gen
) {
22 std::vector
<T
> data(n
);
23 std::generate(data
.begin(), data
.end(), gen
);
26 LzwEncode
<T
>(data
.begin(), data
.end(), std::back_inserter(lzw
));
28 std::vector
<T
> unlzw(data
.size() * 2);
29 auto unlzw_end
= LzwDecode
<T
>(lzw
.begin(), lzw
.end(), unlzw
.data());
30 unlzw
.resize(unlzw_end
- unlzw
.data());
32 EXPECT_EQ(data
, unlzw
);
36 static constexpr size_t kSizes
[] = {0, 1, 2, 7, 13, 32, 129, 10000};
38 using LzwTestTypes
= ::testing::Types
<u8
, u16
, u32
, u64
>;
39 TYPED_TEST_SUITE(LzwTest
, LzwTestTypes
, );
41 TYPED_TEST(LzwTest
, Same
) {
42 MurMur2Hash64Builder
h(0);
43 for (size_t sz
: kSizes
) {
45 for (size_t i
= 0; i
< 100 && !this->HasFailure(); ++i
) {
46 this->Run(sz
, [&] { return v
; });
53 TYPED_TEST(LzwTest
, Increment
) {
54 MurMur2Hash64Builder
h(0);
55 for (size_t sz
: kSizes
) {
57 for (size_t i
= 0; i
< 100 && !this->HasFailure(); ++i
) {
58 this->Run(sz
, [&v
] { return v
++; });
65 TYPED_TEST(LzwTest
, IncrementMod
) {
66 MurMur2Hash64Builder
h(0);
67 for (size_t sz
: kSizes
) {
69 for (size_t i
= 1; i
< 16 && !this->HasFailure(); ++i
) {
70 this->Run(sz
, [&] { return v
++ % i
; });
77 TYPED_TEST(LzwTest
, RandomLimited
) {
78 for (size_t sz
: kSizes
) {
79 for (size_t i
= 1; i
< 1000 && !this->HasFailure(); i
*= 2) {
82 MurMur2Hash64Builder
h(v
% i
/* Keep unique set limited */);
90 } // namespace __sanitizer