Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rigtorp committed Nov 16, 2010
1 parent 9fa8076 commit 6f10826
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,15 @@


CXXFLAGS = -Wall -O3 -Iinclude 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) all: $(TARGETS)


$(TARGETS): include/nmq.hpp $(TARGETS): include/nmq.hpp
tests: LDFLAGS += -lgtest -lpthread

test: tests
@./tests


clean: clean:
rm -f *~ core rm -f *~ core
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ context switches.
Building Building
-------- --------


Just run make. Requires recent GCC. Just run make. Requires recent GCC. Tests require Google Test.


Performance Performance
----------- -----------
Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
* Ping nodes to check if they are running * Ping nodes to check if they are running
* Unit tests * Unit tests
* Maybe allow zero length messages * 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
2 changes: 1 addition & 1 deletion include/nmq.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class context_t {


size_t recv_size; size_t recv_size;
memcpy(&recv_size, d, sizeof(size_t)); memcpy(&recv_size, d, sizeof(size_t));
assert(recv_size >= *size && "buffer too small"); assert(recv_size <= *size && "buffer too small");
*size = recv_size; *size = recv_size;
memcpy(msg, d + sizeof(size_t), recv_size); memcpy(msg, d + sizeof(size_t), recv_size);


Expand Down
61 changes: 61 additions & 0 deletions tests.cpp
Original file line number Original file line Diff line number Diff line change
@@ -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.