Skip to content

Commit 4d99677

Browse files
committed
Add a test for API code
1 parent 73bd007 commit 4d99677

File tree

4 files changed

+517
-16
lines changed

4 files changed

+517
-16
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ set(TEST_SOURCES
7373
test/TestCompressedStream.cpp
7474
test/TestDefaultBitStream.cpp
7575
test/TestTransforms.cpp
76+
test/TestAPI.cpp
7677
)
7778

7879
set(APP_SOURCES
@@ -110,6 +111,9 @@ target_link_libraries(testDefaultBitStream libkanzi)
110111
add_executable(testCompressedStream test/TestCompressedStream.cpp)
111112
target_link_libraries(testCompressedStream libkanzi)
112113

114+
add_executable(testAPI test/TestAPI.cpp)
115+
target_link_libraries(testAPI libkanzi)
116+
113117
# Main executable
114118

115119
# Dynamically linked executable
@@ -128,7 +132,7 @@ target_link_libraries(kanzi_static libkanzi)
128132

129133
# Custom target to build all tests
130134
add_custom_target(test
131-
DEPENDS testBWT testTransforms testEntropyCodec testDefaultBitStream testCompressedStream
135+
DEPENDS testBWT testTransforms testEntropyCodec testDefaultBitStream testCompressedStream testAPI
132136
)
133137
# Custom target to build static libraries
134138
add_custom_target(static_lib

src/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#CXX=clang++
1+
CXX=clang++
22
OBJ_DIR = obj
33
TEST_OBJ_DIR=$(OBJ_DIR)/test
44
APP=kanzi
@@ -25,7 +25,7 @@ else
2525
CXXFLAGS=-c -std=c++14 -Wall -Wextra -O3 -fomit-frame-pointer -fPIC -DNDEBUG -pedantic -march=native -fno-rtti $(CONCURRENCY_FLAG)
2626
else
2727
#CXXFLAGS=-c -std=c++17 -fsanitize=signed-integer-overflow -ftrapv -D_FORTIFY_SOURCE=3 -Wall -Wextra -O3 -fPIC -DNDEBUG -pedantic -fno-rtti $(CONCURRENCY_FLAG)
28-
CXXFLAGS=-c -std=c++17 -fstrict-aliasing -Wall -Wextra -Wpedantic -Wdeprecated -O3 -fPIC -DNDEBUG -pedantic -fno-rtti $(CONCURRENCY_FLAG)
28+
CXXFLAGS=-c -std=c++17 -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST -fstrict-aliasing -Wall -Wextra -Wpedantic -Wdeprecated -O3 -fPIC -DNDEBUG -pedantic -fno-rtti $(CONCURRENCY_FLAG)
2929
endif
3030
endif
3131

@@ -151,6 +151,9 @@ $(SHARED_DECOMP_LIB):$(LIB_DECOMP_OBJECTS)
151151

152152

153153
# Targets for test executables
154+
testAPI: $(LIB_OBJECTS) $(TEST_OBJ_DIR)/TestAPI.o
155+
$(CXX) $^ -o ../bin/$@ $(LDFLAGS)
156+
154157
testBWT: $(LIB_OBJECTS) $(TEST_OBJ_DIR)/TestBWT.o
155158
$(CXX) $^ -o ../bin/$@ $(LDFLAGS)
156159

@@ -166,7 +169,7 @@ testDefaultBitStream: $(LIB_OBJECTS) $(TEST_OBJ_DIR)/TestDefaultBitStream.o
166169
testCompressedStream: $(LIB_OBJECTS) $(TEST_OBJ_DIR)/TestCompressedStream.o
167170
$(CXX) $^ -o ../bin/$@ $(LDFLAGS)
168171

169-
test: testBWT testTransforms testEntropyCodec testDefaultBitStream testCompressedStream
172+
test: testAPI testBWT testTransforms testEntropyCodec testDefaultBitStream testCompressedStream
170173

171174
# Target for the main application
172175
kanzi: $(LIB_OBJECTS) $(APP_OBJECTS)

src/api/Decompressor.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ using namespace std;
3737
using namespace kanzi;
3838

3939
namespace kanzi {
40+
4041
class ifstreambuf : public streambuf {
4142
public:
4243
ifstreambuf(int fd) : _fd(fd) {
43-
setg(&_buffer[4], &_buffer[4], &_buffer[4]);
44+
// gptr() = egptr() initially forces underflow() on first read
45+
setg(_buffer + 4, _buffer + 4, _buffer + 4);
4446
}
4547

4648
private:
@@ -52,28 +54,26 @@ namespace kanzi {
5254
if (gptr() < egptr())
5355
return traits_type::to_int_type(*gptr());
5456

55-
// Number of characters to preserve (putback)
57+
// Preserve up to 4 characters for putback
5658
int putback = int(gptr() - eback());
5759

5860
if (putback > 4) putback = 4;
5961

60-
// Prevent reading before buffer start
61-
const char* src = gptr() - putback;
62-
63-
if (src < _buffer) {
64-
putback = int(gptr() - _buffer);
65-
src = _buffer;
62+
// Only move putback if > 0
63+
if (putback > 0) {
64+
std::memmove(_buffer + (4 - putback), gptr() - putback, putback);
6665
}
6766

68-
// Move putback characters to start of buffer
69-
std::memmove(_buffer + (4 - putback), src, putback);
70-
71-
// Read new characters into buffer
67+
// Read new data
7268
const int n = int(READ(_fd, _buffer + 4, BUF_SIZE - 4));
7369

7470
if (n <= 0)
7571
return EOF;
7672

73+
// Reset get pointers:
74+
// eback = start of buffer (including putback area)
75+
// gptr = first new byte
76+
// egptr = end of new data
7777
setg(_buffer + (4 - putback), _buffer + 4, _buffer + 4 + n);
7878
return traits_type::to_int_type(*gptr());
7979
}

0 commit comments

Comments
 (0)