Permalink
Browse files

Added some unit tests and license

  • Loading branch information...
1 parent 0c2fb4f commit 6afcf21ce3763e5c91662e9d93e24e87d410613b @lalinsky lalinsky committed Jan 30, 2011
View
@@ -6,3 +6,5 @@ CMakeCache.txt
./fpserver
./fpindex
./fpsearch
+./tests
+doc/tree_structure.png
View
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.6)
project(fpserver)
+find_package(GTest)
find_package(Qt4 COMPONENTS QtCore QtNetwork REQUIRED)
include(${QT_USE_FILE})
@@ -19,8 +20,11 @@ set(fpserver_SOURCES listener.cpp connection.cpp main.cpp)
qt4_wrap_cpp(fpserver_MOC ${fpserver_HEADERS})
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
-include_directories(${CMAKE_CURRENT_BINARY_DIR})
+include_directories(
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ ${GTEST_INCLUDE_DIRS}
+)
add_executable(fpserver
${fpserver_SOURCES}
@@ -34,3 +38,15 @@ target_link_libraries(fpindex ${QT_LIBRARIES} fpindexlib)
add_executable(fpsearch fpsearch.cpp)
target_link_libraries(fpsearch ${QT_LIBRARIES} fpindexlib)
+set(tests_SOURCES
+ src/store/input_stream_test.cpp
+ src/store/output_stream_test.cpp
+ src/util/tests.cpp
+)
+
+if(GTEST_FOUND)
+ add_executable(tests ${tests_SOURCES})
+ target_link_libraries(tests ${GTEST_BOTH_LIBRARIES} -lpthread fpindexlib)
+ add_custom_target(check ./tests $ENV{GTEST_FLAGS} DEPENDS tests)
+endif()
+
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
Binary file not shown.
View
@@ -1,3 +1,19 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
#include "input_stream.h"
InputStream::~InputStream()
@@ -18,9 +34,11 @@ uint32_t InputStream::readVInt32()
{
uint8_t b = readByte();
uint32_t i = b & 0x7f;
+ int shift = 7;
while (b & 0x80) {
b = readByte();
- i = (i << 7) | (b & 0x7f);
+ i |= (b & 0x7f) << shift;
+ shift += 7;
}
return i;
}
View
@@ -1,3 +1,19 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
#ifndef ACOUSTID_INPUT_STREAM_H_
#define ACOUSTID_INPUT_STREAM_H_
@@ -0,0 +1,86 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include <gtest/gtest.h>
+#include "input_stream.h"
+
+class SimpleInputStream : public InputStream
+{
+public:
+ SimpleInputStream(uint8_t *data) : m_data(data) { }
+
+ uint8_t readByte()
+ {
+ return *m_data++;
+ }
+
+ size_t position()
+ {
+ return 0;
+ }
+
+ void seek(size_t)
+ {
+ }
+
+private:
+ uint8_t *m_data;
+};
+
+TEST(InputStream, ReadByte)
+{
+ uint8_t data[] = { 0, 0xff, 0x01 };
+ SimpleInputStream inputStream(data);
+ ASSERT_EQ(0x00, inputStream.readByte());
+ ASSERT_EQ(0xff, inputStream.readByte());
+ ASSERT_EQ(0x01, inputStream.readByte());
+}
+
+TEST(InputStream, ReadInt16)
+{
+ uint8_t data[] = { 0, 0, 0xff, 0xff, 0x01, 0x02 };
+ SimpleInputStream inputStream(data);
+ ASSERT_EQ(0x0000, inputStream.readInt16());
+ ASSERT_EQ(0xffff, inputStream.readInt16());
+ ASSERT_EQ(0x0102, inputStream.readInt16());
+}
+
+TEST(InputStream, ReadInt32)
+{
+ uint8_t data[] = { 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04 };
+ SimpleInputStream inputStream(data);
+ ASSERT_EQ(0x00000000, inputStream.readInt32());
+ ASSERT_EQ(0xffffffff, inputStream.readInt32());
+ ASSERT_EQ(0x01020304, inputStream.readInt32());
+}
+
+TEST(InputStream, ReadVInt32)
+{
+ uint8_t data[] = {
+ 1,
+ 0x80 | 1, 2,
+ 0x80 | 1, 0x80 | 2, 3,
+ 0x80 | 1, 0x80 | 2, 0x80 | 3, 4,
+ 0x80 | 1, 0x80 | 2, 0x80 | 3, 0x80 | 4, 5,
+ };
+ SimpleInputStream inputStream(data);
+ ASSERT_EQ(1, inputStream.readVInt32());
+ ASSERT_EQ((2 << 7) | 1, inputStream.readVInt32());
+ ASSERT_EQ((3 << 14) | (2 << 7) | 1, inputStream.readVInt32());
+ ASSERT_EQ((4 << 21) | (3 << 14) | (2 << 7) | 1, inputStream.readVInt32());
+ ASSERT_EQ((5 << 28) | (4 << 21) | (3 << 14) | (2 << 7) | 1, inputStream.readVInt32());
+}
+
@@ -1,3 +1,19 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
#include "output_stream.h"
OutputStream::~OutputStream()
View
@@ -1,3 +1,19 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
#ifndef ACOUSTID_OUTPUT_STREAM_H_
#define ACOUSTID_OUTPUT_STREAM_H_
@@ -10,13 +26,13 @@ class OutputStream {
virtual ~OutputStream();
virtual void writeByte(uint8_t value) = 0;
- virtual void writeInt16(uint16_t value) = 0;
- virtual void writeInt32(uint32_t value) = 0;
- virtual void writeVInt32(uint32_t value) = 0;
+ virtual void writeInt16(uint16_t value);
+ virtual void writeInt32(uint32_t value);
+ virtual void writeVInt32(uint32_t value);
virtual size_t position() = 0;
virtual void seek(size_t position) = 0;
- virtual void flush() = 0;
+ virtual void flush() {};
};
@@ -0,0 +1,146 @@
+// Acoustid Index -- Inverted index for audio fingerprints
+// Copyright (C) 2011 Lukas Lalinsky
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include <gtest/gtest.h>
+#include "util/test_utils.h"
+#include "output_stream.h"
+
+class SimpleOutputStream : public OutputStream
+{
+public:
+ SimpleOutputStream(uint8_t *data) : m_data(data), m_length(0) { }
+
+ void reset()
+ {
+ m_length = 0;
+ }
+
+ void writeByte(uint8_t b)
+ {
+ m_data[m_length++] = b;
+ }
+
+ size_t position()
+ {
+ return m_length;
+ }
+
+ void seek(size_t)
+ {
+ }
+
+private:
+ size_t m_length;
+ uint8_t *m_data;
+};
+
+TEST(OutputStream, WriteByte)
+{
+ uint8_t data[100];
+ uint8_t expected[] = { 1, 255 };
+ SimpleOutputStream outputStream(data);
+
+ outputStream.reset();
+ outputStream.writeByte(1);
+ ASSERT_EQ(1, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[0], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeByte(255);
+ ASSERT_EQ(1, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[1], data, outputStream.position());
+}
+
+TEST(OutputStream, WriteInt16)
+{
+ uint8_t data[100];
+ uint8_t expected[] = { 0, 1, /**/ 1, 0, /**/ 255, 255 };
+ SimpleOutputStream outputStream(data);
+
+ outputStream.reset();
+ outputStream.writeInt16(1);
+ ASSERT_EQ(2, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[0], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeInt16(256);
+ ASSERT_EQ(2, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[2], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeInt16(0xffff);
+ ASSERT_EQ(2, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[4], data, outputStream.position());
+}
+
+TEST(OutputStream, WriteInt32)
+{
+ uint8_t data[100];
+ uint8_t expected[] = { 0, 0, 0, 1, /**/ 0, 0, 1, 0, /**/ 0, 0, 255, 255,
+ 255, 255, 255, 255 };
+ SimpleOutputStream outputStream(data);
+
+ outputStream.reset();
+ outputStream.writeInt32(1);
+ ASSERT_EQ(4, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[0], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeInt32(256);
+ ASSERT_EQ(4, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[4], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeInt32(0xffff);
+ ASSERT_EQ(4, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[8], data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeInt32(0xffffffff);
+ ASSERT_EQ(4, outputStream.position());
+ ASSERT_INTARRAY_EQ(&expected[12], data, outputStream.position());
+}
+
+TEST(OutputStream, WriteVInt32)
+{
+ uint8_t data[100];
+ SimpleOutputStream outputStream(data);
+
+ outputStream.reset();
+ outputStream.writeVInt32(0);
+ uint8_t expected_0[] = { 0 };
+ ASSERT_EQ(1, outputStream.position());
+ ASSERT_INTARRAY_EQ(expected_0, data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeVInt32(1);
+ uint8_t expected_1[] = { 1 };
+ ASSERT_EQ(1, outputStream.position());
+ ASSERT_INTARRAY_EQ(expected_1, data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeVInt32(128);
+ uint8_t expected_128[] = { 128, 1 };
+ ASSERT_EQ(2, outputStream.position());
+ ASSERT_INTARRAY_EQ(expected_128, data, outputStream.position());
+
+ outputStream.reset();
+ outputStream.writeVInt32(16385);
+ uint8_t expected_16385[] = { 129, 128, 1 };
+ ASSERT_EQ(3, outputStream.position());
+ ASSERT_INTARRAY_EQ(expected_16385, data, outputStream.position());
+}
+
Oops, something went wrong.

0 comments on commit 6afcf21

Please sign in to comment.