Skip to content

Commit 2d6f3c3

Browse files
committed
Reapply "[Mangler] Add unit tests to the compression routines.""
This reverts commit f4ccef2.
1 parent f57668f commit 2d6f3c3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

unittests/Basic/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_swift_unittest(SwiftBasicTests
2020
Unicode.cpp
2121
BlotMapVectorTest.cpp
2222
PointerIntEnumTest.cpp
23+
CompressionTests.cpp
2324
${generated_tests}
2425
)
2526

unittests/Basic/CompressionTests.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===- CompressionTests.cpp - for swift/ABI/Compression.h -----------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#include "swift/ABI/Compression.h"
13+
#include "gtest/gtest.h"
14+
15+
using namespace swift::Compress;
16+
17+
const char* TestValues[] = {"AA", "r", "J", "Swift","A", "ArrayStringPrintable",
18+
"AB","JA","YA","encodeCBCString", "HelloWorld", "long", "_TThisIsATestString"
19+
"Done", "Smile","_S_S_S", "________", "_TSLZ","Lempel_Ziv", "Ziv_and_Lempel",
20+
"JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ",
21+
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
22+
"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
23+
"AllDone", "UnderWaterCordlessDrill","UnderWaterAngleGrinder", "Printable", "Clone", "Collection"
24+
"__TwxxV14StdlibUnittest24MinimalForwardCollection",
25+
"__TMPVs15ContiguousArray",
26+
"__TIF14StdlibUnittest13checkSequenceu0_Rxs14CollectionType_s12SequenceTypeWx9Generator7Element_zW_9GeneratorS3__rFTxq",
27+
"__TTSg5VSS13CharacterViewS_s14CollectionTypes_GVs17IndexingGeneratorS__GS1_S__s13GeneratorTypes_VS_5IndexS3_s16Forwar",
28+
""};
29+
30+
// Test that the code book compression round trips.
31+
TEST(Compression, RoundTripCBC) {
32+
for (const char* N : TestValues) {
33+
std::string encoded = EncodeCBCString(N);
34+
std::string decoded = DecodeCBCString(encoded);
35+
EXPECT_EQ(decoded, std::string(N));
36+
}
37+
}
38+
39+
// Test flat (non variable length) encoding.
40+
TEST(Compression, FlatEncoding) {
41+
for (const char* input : TestValues) {
42+
llvm::APInt flat_code = EncodeStringAsNumber(input, EncodingKind::Fixed);
43+
std::string flat_input = DecodeStringFromNumber(flat_code, EncodingKind::Fixed);
44+
EXPECT_EQ(flat_input, input);
45+
}
46+
47+
// Check that we can encode and decode all numbers and that we get the
48+
// correct value after round trips.
49+
for (int i = 0; i < 10000; i++) {
50+
llvm::APInt num = llvm::APInt(64, i);
51+
std::string encoded = DecodeStringFromNumber(num, EncodingKind::Fixed);
52+
llvm::APInt decoded_num = EncodeStringAsNumber(encoded, EncodingKind::Fixed);
53+
EXPECT_EQ(num.getZExtValue(), decoded_num.getZExtValue());
54+
}
55+
}
56+
57+
// Test variable length encoding.
58+
TEST(Compression, VarEncoding) {
59+
for (const char* input : TestValues) {
60+
llvm::APInt var_code = EncodeStringAsNumber(input, EncodingKind::Variable);
61+
std::string var_input = DecodeStringFromNumber(var_code, EncodingKind::Variable);
62+
EXPECT_EQ(var_input, input);
63+
}
64+
}
65+
66+
TEST(Compression, VariableLength) {
67+
for (const char* input : TestValues) {
68+
llvm::APInt code = EncodeStringAsNumber(input, EncodingKind::Variable);
69+
70+
std::string encoded = DecodeStringFromNumber(code, EncodingKind::Fixed);
71+
llvm::APInt code2 = EncodeStringAsNumber(encoded, EncodingKind::Fixed);
72+
73+
std::string encoded2 = DecodeStringFromNumber(code2, EncodingKind::Fixed);
74+
llvm::APInt code3 = EncodeStringAsNumber(encoded2, EncodingKind::Fixed);
75+
EXPECT_EQ(code.toString(10, false), code2.toString(10, false));
76+
EXPECT_EQ(code3, code2);
77+
78+
std::string decoded = DecodeStringFromNumber(code2, EncodingKind::Variable);
79+
EXPECT_EQ(decoded, input);
80+
}
81+
}
82+
83+
TEST(Compression, FullCompression) {
84+
for (const char* input : TestValues) {
85+
std::string compressed = CompressName(input);
86+
std::string decompressed = DecompressName(compressed);
87+
EXPECT_EQ(std::string(input), decompressed);
88+
}
89+
}

0 commit comments

Comments
 (0)