Skip to content

Commit

Permalink
Add unit tests (madler#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- authored Feb 19, 2017
1 parent 00eff18 commit 261b8d0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ compiler:
script:
- mkdir build
- cd build
- cmake .. && make && make test
- cmake -DCMAKE_BUILD_TYPE=Debug .. && make && make test
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ if (MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
endif (MSVC)

# libzip
# zip
set(SRC src/miniz.h src/zip.h src/zip.c)
add_library(${CMAKE_PROJECT_NAME} ${SRC})

# zip_test
add_executable(zip_test test/main.c)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(zip_test ${CMAKE_PROJECT_NAME})

# test
enable_testing()
add_test(NAME zip_test COMMAND zip_test)
add_subdirectory(test)

5 changes: 3 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ build_script:
cd build
cmake -G"Visual Studio 14" ..
cmake -G"Visual Studio 14" -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config %CMAKE_BUILD_TYPE%
ctest --verbose -C "Debug"
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 2.8)

# test
include_directories(../src)
add_executable(testzip test.c ../src/zip.c)

add_test(NAME test COMMAND testzip)
96 changes: 0 additions & 96 deletions test/main.c

This file was deleted.

60 changes: 60 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <zip.h>

#include <assert.h>
#include <stdio.h>
#include <string.h>

#define ZIPNAME "test.zip"
#define TESTDATA1 "Some test data 1...\0"
#define TESTDATA2 "Some test data 2...\0"

static void test_write(void) {
struct zip_t *zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
assert(zip != NULL);

assert(0 == zip_entry_open(zip, "test-1.txt"));
assert(0 == zip_entry_write(zip, TESTDATA1, strlen(TESTDATA1)));
assert(0 == zip_entry_close(zip));

zip_close(zip);
}

static void test_append(void) {
struct zip_t *zip = zip_open(ZIPNAME, ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
assert(zip != NULL);

assert(0 == zip_entry_open(zip, "test-2.txt"));
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
assert(0 == zip_entry_close(zip));

zip_close(zip);
}

static void test_read(void) {
char *buf = NULL;
size_t bufsize;
struct zip_t *zip = zip_open(ZIPNAME, 0, 'r');
assert(zip != NULL);

assert(0 == zip_entry_open(zip, "test-1.txt"));
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
assert(bufsize == strlen(TESTDATA1));
assert(0 == strncmp(buf, TESTDATA1, bufsize));
assert(0 == zip_entry_close(zip));

assert(0 == zip_entry_open(zip, "test-2.txt"));
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
assert(bufsize == strlen(TESTDATA2));
assert(0 == strncmp(buf, TESTDATA2, bufsize));
assert(0 == zip_entry_close(zip));

zip_close(zip);
}

int main(int argc, char *argv[]) {
test_write();
test_append();
test_read();

return remove(ZIPNAME);
}

0 comments on commit 261b8d0

Please sign in to comment.