Permalink
Browse files

Add some unit tests

  • Loading branch information...
1 parent 9fa8076 commit 6f108263c6ba35dbd4008e763d8ca503fd688beb @rigtorp committed Nov 16, 2010
Showing with 70 additions and 4 deletions.
  1. +5 −1 Makefile
  2. +1 −1 README
  3. +2 −1 TODO
  4. +1 −1 include/nmq.hpp
  5. +61 −0 tests.cpp
View
@@ -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
View
2 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
-----------
View
3 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
View
@@ -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);
View
@@ -0,0 +1,61 @@
+/*
+ Copyright (C) 2010 Erik Rigtorp <erik@rigtorp.com>.
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include <gtest/gtest.h>
+#include <nmq.hpp>
+
+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();
+}

0 comments on commit 6f10826

Please sign in to comment.