Skip to content

Commit 3378032

Browse files
authored
feat: Add Test and GitHub Workflows
1 parent b169f7e commit 3378032

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

.github/workflows/test.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: LZW
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
pull_request:
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
name: build
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: build
20+
run: |
21+
mkdir build
22+
cd build
23+
cmake ..
24+
make
25+
26+
test:
27+
name: test
28+
runs-on: ubuntu-latest
29+
30+
needs: build
31+
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@v2
35+
36+
- name: Run tests
37+
run: |
38+
cd tests
39+
mkdir build
40+
cd build
41+
cmake ..
42+
make lzw_test
43+
./lzw_test

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(LZW_CPP)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
add_library(LZW STATIC src/LZW.cpp src/LZW.h)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Lempel–Ziv–Welch Compression in CPP
2-
[![core-build](https://github.com/Gozzim/LZW_CPP/actions/workflows/core-build.yml/badge.svg?branch=master)](https://github.com/Gozzim/LZW_CPP)
2+
[![core-build](https://github.com/Gozzim/LZW_CPP/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/Gozzim/LZW_CPP)
33
[![CodeFactor](https://www.codefactor.io/repository/github/gozzim/lzw_cpp/badge?s=b829f1f733eba50c4a453362dbd965c4e819270a)](https://www.codefactor.io/repository/github/gozzim/lzw_cpp)
44

55
Implementation of LZW (Lempel–Ziv–Welch) compression and decompression in C++.

tests/CMakeLists.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(LZWTest)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
googletest
10+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
11+
)
12+
13+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
14+
FetchContent_MakeAvailable(googletest)
15+
16+
enable_testing()
17+
18+
add_executable(lzw_test test.cpp ../src/LZW.cpp)
19+
20+
target_link_libraries(lzw_test GTest::gtest_main)
21+
22+
include(GoogleTest)
23+
gtest_discover_tests(lzw_test)

tests/test.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "../src/LZW.h"
2+
#include <chrono>
3+
#include <gtest/gtest.h>
4+
#include <iostream>
5+
#include <random>
6+
7+
TEST(LZWTest, CompressionAndDecompression)
8+
{
9+
unsigned int seed = static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count());
10+
std::mt19937 rng(seed);
11+
std::uniform_int_distribution<int> distribution(0, 255);
12+
std::string input1;
13+
std::string input2;
14+
std::string input3;
15+
std::string input4;
16+
17+
for (int i = 1; i <= 1000000; ++i) {
18+
input1 += static_cast<char>(distribution(rng));
19+
input2 += static_cast<char>(distribution(rng) % 128);
20+
input3 += static_cast<char>(i % 256);
21+
input4 += static_cast<char>(100);
22+
}
23+
24+
auto [compressResult1, compressCode1] = sLZW->Compress(input1);
25+
ASSERT_EQ(compressCode1, LZW_OK);
26+
auto [compressResult2, compressCode2] = sLZW->Compress(input2);
27+
ASSERT_EQ(compressCode2, LZW_OK);
28+
auto [compressResult3, compressCode3] = sLZW->Compress(input3);
29+
ASSERT_EQ(compressCode3, LZW_OK);
30+
auto [compressResult4, compressCode4] = sLZW->Compress(input4);
31+
ASSERT_EQ(compressCode4, LZW_OK);
32+
33+
auto [decompressResult1, decompressCode1] = sLZW->Decompress(compressResult1);
34+
ASSERT_EQ(decompressCode1, LZW_OK);
35+
auto [decompressResult2, decompressCode2] = sLZW->Decompress(compressResult2);
36+
ASSERT_EQ(decompressCode2, LZW_OK);
37+
auto [decompressResult3, decompressCode3] = sLZW->Decompress(compressResult3);
38+
ASSERT_EQ(decompressCode3, LZW_OK);
39+
auto [decompressResult4, decompressCode4] = sLZW->Decompress(compressResult4);
40+
ASSERT_EQ(decompressCode4, LZW_OK);
41+
42+
ASSERT_EQ(decompressResult1, input1);
43+
ASSERT_EQ(decompressResult2, input2);
44+
ASSERT_EQ(decompressResult3, input3);
45+
ASSERT_EQ(decompressResult4, input4);
46+
}
47+
48+
int main(int argc, char **argv)
49+
{
50+
::testing::InitGoogleTest(&argc, argv);
51+
return RUN_ALL_TESTS();
52+
}

0 commit comments

Comments
 (0)