From 6f108263c6ba35dbd4008e763d8ca503fd688beb Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Mon, 15 Nov 2010 21:01:31 -0500 Subject: [PATCH] Add some unit tests --- Makefile | 6 ++++- README | 2 +- TODO | 3 ++- include/nmq.hpp | 2 +- tests.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests.cpp diff --git a/Makefile b/Makefile index f61ced1..ba75402 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,15 @@ CXXFLAGS = -Wall -O3 -Iinclude -TARGETS = local_lat remote_lat local_thr remote_thr +TARGETS = local_lat remote_lat local_thr remote_thr tests all: $(TARGETS) $(TARGETS): include/nmq.hpp +tests: LDFLAGS += -lgtest -lpthread + +test: tests + @./tests clean: rm -f *~ core diff --git a/README b/README index 44743f9..d78d9b7 100644 --- a/README +++ b/README @@ -12,7 +12,7 @@ context switches. Building -------- -Just run make. Requires recent GCC. +Just run make. Requires recent GCC. Tests require Google Test. Performance ----------- diff --git a/TODO b/TODO index 635b4a4..6ec1b0c 100644 --- a/TODO +++ b/TODO @@ -4,4 +4,5 @@ * Ping nodes to check if they are running * Unit tests * Maybe allow zero length messages -* Magic and versioning of mmap file +* Magic and version of mmap() file +* Support context using mkstemp() for in process communication and testing \ No newline at end of file diff --git a/include/nmq.hpp b/include/nmq.hpp index 9975e77..8154ebc 100644 --- a/include/nmq.hpp +++ b/include/nmq.hpp @@ -207,7 +207,7 @@ class context_t { size_t recv_size; memcpy(&recv_size, d, sizeof(size_t)); - assert(recv_size >= *size && "buffer too small"); + assert(recv_size <= *size && "buffer too small"); *size = recv_size; memcpy(msg, d + sizeof(size_t), recv_size); diff --git a/tests.cpp b/tests.cpp new file mode 100644 index 0000000..23c2165 --- /dev/null +++ b/tests.cpp @@ -0,0 +1,61 @@ +/* + Copyright (C) 2010 Erik Rigtorp . + All rights reserved. + + This file is part of NanoMQ. + + NanoMQ 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. + + NanoMQ 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 NanoMQ. If not, see . +*/ + +#include +#include + +TEST(Tests, Test) { + + // Open context + char *fname = tempnam(NULL, "nmq"); // UGLY + ASSERT_NE(NULL, (size_t)fname); + nmq::context_t context(fname); + ASSERT_TRUE(context.create(4, 10, 100)); + + nmq::node_t node0(context, 0); + nmq::node_t node1(context, 1); + nmq::node_t node2(context, 2); + nmq::node_t node3(context, 3); + + char buf[100]; + size_t sz = 100; + node0.send(1, "test", 5); + node1.recv(0, &buf, &sz); + EXPECT_EQ(5, sz); + EXPECT_STREQ("test", buf); + + node0.send(2, "test", 5); + node2.recv(0, &buf, &sz); + EXPECT_EQ(5, sz); + EXPECT_STREQ("test", buf); + + node0.send(3, "test", 5); + node3.recv(0, &buf, &sz); + EXPECT_EQ(5, sz); + EXPECT_STREQ("test", buf); + + ASSERT_EQ(0, unlink(fname)); +} + + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}