Skip to content

Commit

Permalink
feat: Add Test and GitHub Workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozzim committed Oct 1, 2023
1 parent b169f7e commit 3378032
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: LZW

on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

jobs:
build:
name: build
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: build
run: |
mkdir build
cd build
cmake ..
make
test:
name: test
runs-on: ubuntu-latest

needs: build

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Run tests
run: |
cd tests
mkdir build
cd build
cmake ..
make lzw_test
./lzw_test
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.14)
project(LZW_CPP)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(LZW STATIC src/LZW.cpp src/LZW.h)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Lempel–Ziv–Welch Compression in CPP
[![core-build](https://github.com/Gozzim/LZW_CPP/actions/workflows/core-build.yml/badge.svg?branch=master)](https://github.com/Gozzim/LZW_CPP)
[![core-build](https://github.com/Gozzim/LZW_CPP/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/Gozzim/LZW_CPP)
[![CodeFactor](https://www.codefactor.io/repository/github/gozzim/lzw_cpp/badge?s=b829f1f733eba50c4a453362dbd965c4e819270a)](https://www.codefactor.io/repository/github/gozzim/lzw_cpp)

Implementation of LZW (Lempel–Ziv–Welch) compression and decompression in C++.
Expand Down
23 changes: 23 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.14)
project(LZWTest)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(lzw_test test.cpp ../src/LZW.cpp)

target_link_libraries(lzw_test GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(lzw_test)
52 changes: 52 additions & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "../src/LZW.h"
#include <chrono>
#include <gtest/gtest.h>
#include <iostream>
#include <random>

TEST(LZWTest, CompressionAndDecompression)
{
unsigned int seed = static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count());
std::mt19937 rng(seed);
std::uniform_int_distribution<int> distribution(0, 255);
std::string input1;
std::string input2;
std::string input3;
std::string input4;

for (int i = 1; i <= 1000000; ++i) {
input1 += static_cast<char>(distribution(rng));
input2 += static_cast<char>(distribution(rng) % 128);
input3 += static_cast<char>(i % 256);
input4 += static_cast<char>(100);
}

auto [compressResult1, compressCode1] = sLZW->Compress(input1);
ASSERT_EQ(compressCode1, LZW_OK);
auto [compressResult2, compressCode2] = sLZW->Compress(input2);
ASSERT_EQ(compressCode2, LZW_OK);
auto [compressResult3, compressCode3] = sLZW->Compress(input3);
ASSERT_EQ(compressCode3, LZW_OK);
auto [compressResult4, compressCode4] = sLZW->Compress(input4);
ASSERT_EQ(compressCode4, LZW_OK);

auto [decompressResult1, decompressCode1] = sLZW->Decompress(compressResult1);
ASSERT_EQ(decompressCode1, LZW_OK);
auto [decompressResult2, decompressCode2] = sLZW->Decompress(compressResult2);
ASSERT_EQ(decompressCode2, LZW_OK);
auto [decompressResult3, decompressCode3] = sLZW->Decompress(compressResult3);
ASSERT_EQ(decompressCode3, LZW_OK);
auto [decompressResult4, decompressCode4] = sLZW->Decompress(compressResult4);
ASSERT_EQ(decompressCode4, LZW_OK);

ASSERT_EQ(decompressResult1, input1);
ASSERT_EQ(decompressResult2, input2);
ASSERT_EQ(decompressResult3, input3);
ASSERT_EQ(decompressResult4, input4);
}

int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 3378032

Please sign in to comment.