mirror of
https://github.com/QuasarApp/zip.git
synced 2025-04-27 04:54:30 +00:00
Add unit tests (#8)
This commit is contained in:
parent
00eff1893f
commit
261b8d040e
@ -7,4 +7,4 @@ compiler:
|
||||
script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake .. && make && make test
|
||||
- cmake -DCMAKE_BUILD_TYPE=Debug .. && make && make test
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
test/CMakeLists.txt
Normal file
7
test/CMakeLists.txt
Normal file
@ -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
test/main.c
96
test/main.c
@ -1,96 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zip.h>
|
||||
|
||||
// callback function
|
||||
int on_extract_entry(const char *filename, void *arg) {
|
||||
static int i = 0;
|
||||
int n = *(int *)arg;
|
||||
printf("Extracted: %s (%d of %d)\n", filename, ++i, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
/*
|
||||
Create a new zip archive with default compression level (6)
|
||||
*/
|
||||
struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
|
||||
// we should check if zip is NULL and if any other function returned < 0
|
||||
{
|
||||
zip_entry_open(zip, "foo-1.txt");
|
||||
{
|
||||
char *buf = "Some data here...";
|
||||
zip_entry_write(zip, buf, strlen(buf));
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
|
||||
zip_entry_open(zip, "foo-2.txt");
|
||||
{
|
||||
// merge 3 files into one entry and compress them on-the-fly.
|
||||
zip_entry_fwrite(zip, "foo-2.1.txt");
|
||||
zip_entry_fwrite(zip, "foo-2.2.txt");
|
||||
zip_entry_fwrite(zip, "foo-2.3.txt");
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
}
|
||||
// always remember to close and release resources
|
||||
zip_close(zip);
|
||||
|
||||
/*
|
||||
Append to existing zip archive
|
||||
*/
|
||||
zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
|
||||
// we should check if zip is NULL
|
||||
{
|
||||
zip_entry_open(zip, "foo-3.txt");
|
||||
{
|
||||
char *buf = "Append some data here...";
|
||||
zip_entry_write(zip, buf, strlen(buf));
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
}
|
||||
// always remember to close and release resources
|
||||
zip_close(zip);
|
||||
|
||||
/*
|
||||
Extract the zip archive into /tmp folder
|
||||
*/
|
||||
int arg = 2;
|
||||
zip_extract("foo.zip", "/tmp", on_extract_entry, &arg);
|
||||
|
||||
/*
|
||||
...or open the zip archive with only read access
|
||||
*/
|
||||
void *buf = NULL;
|
||||
size_t bufsize;
|
||||
|
||||
zip = zip_open("foo.zip", 0, 'r');
|
||||
// we should check if zip is NULL and if any other function returned < 0
|
||||
{
|
||||
zip_entry_open(zip, "foo-1.txt");
|
||||
{
|
||||
// extract into memory
|
||||
zip_entry_read(zip, &buf, &bufsize);
|
||||
printf("Read(foo-1.txt): %zu bytes: %.*s\n", bufsize, (int)bufsize,
|
||||
buf);
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
|
||||
zip_entry_open(zip, "foo-2.txt");
|
||||
{
|
||||
// extract into a file
|
||||
zip_entry_fread(zip, "foo-2.txt");
|
||||
}
|
||||
zip_entry_close(zip);
|
||||
}
|
||||
// always remember to close and release resources
|
||||
zip_close(zip);
|
||||
|
||||
// do something with buffer... and remember to free memory
|
||||
free(buf);
|
||||
|
||||
return 0;
|
||||
}
|
60
test/test.c
Normal file
60
test/test.c
Normal file
@ -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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user