Skip to content

Commit

Permalink
Add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen committed Jul 5, 2016
1 parent a311af1 commit e858d65
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
build
lib
*~
test
dmlc-core
cli_test
21 changes: 14 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
export LDFLAGS= -pthread -lm
export CFLAGS= -std=c++11 -Wall -O3 -msse2 -Wno-unknown-pragmas -funroll-loops\
export LDFLAGS = -pthread -lm
export CFLAGS = -std=c++11 -Wall -O3 -msse2 -Wno-unknown-pragmas -funroll-loops\
-Iinclude -Idmlc-core/include -fPIC

# specify tensor path
.PHONY: clean all test lint doc


all: lib/libnngraph.so test
all: lib/libnngraph.so lib/libnngraph.a cli_test

SRC = $(wildcard src/*.cc src/*/*.cc example/*.cc)
ALL_OBJ = $(patsubst src/%.cc, build/%.o, $(SRC))
ALL_DEP = $(ALL_OBJ)
ALL_DEP = $(filter-out build/test_main.o, $(ALL_OBJ))

include tests/cpp/unittest.mk

test: $(TEST)

build/%.o: src/%.cc
@mkdir -p $(@D)
Expand All @@ -21,7 +24,11 @@ lib/libnngraph.so: $(ALL_DEP)
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.o %.a, $^) $(LDFLAGS)

test: $(ALL_DEP)
lib/libnngraph.a: $(ALL_DEP)
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)

cli_test: $(ALL_DEP) build/test_main.o
$(CXX) $(CFLAGS) -o $@ $(filter %.o %.a, $^) $(LDFLAGS)

lint:
Expand All @@ -31,7 +38,7 @@ doc:
doxygen docs/Doxyfile

clean:
$(RM) -rf build lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o test
$(RM) -rf build lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o cli_test

-include build/*.d
-include build/*/*.d
3 changes: 3 additions & 0 deletions tests/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
unittest
*.d
*_test
21 changes: 21 additions & 0 deletions tests/cpp/op_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <dmlc/logging.h>
#include <gtest/gtest.h>
#include <nngraph/op.h>
#include <utility>

NNGRAPH_REGISTER_OP(add)
.describe("add two data together")
.set_num_inputs(2)
.attr("inplace_pair", std::make_pair(0, 0));

NNGRAPH_REGISTER_OP(add)
.attr<std::string>("nick_name", "plus");


TEST(Op, GetAttr) {
using namespace nngraph;
auto add = Op::Get("add");
auto nick = Op::GetAttr<std::string>("nick_name");

CHECK_EQ(nick[add], "plus");
}
24 changes: 24 additions & 0 deletions tests/cpp/tuple_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <dmlc/logging.h>
#include <gtest/gtest.h>
#include <nngraph/tuple.h>

TEST(Tuple, Basic) {
using nngraph::Tuple;
using nngraph::TShape;
Tuple<int> x{1, 2, 3};
Tuple<int> y{1, 2, 3, 5, 6};
x = std::move(y);

CHECK_EQ(x.ndim(), 5);
Tuple<int> z{1, 2, 3, 5, 6};
std::ostringstream os;
os << z;
CHECK_EQ(os.str(), "(1,2,3,5,6)");
std::istringstream is(os.str());
is >> y;
CHECK_EQ(x, y);
Tuple<nngraph::index_t> ss{1, 2, 3};
TShape s = ss;
s = std::move(ss);
CHECK((s == TShape{1, 2, 3}));
}
12 changes: 12 additions & 0 deletions tests/cpp/unittest.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GTEST_LIB=$(GTEST_PATH)/lib/
GTEST_INC=$(GTEST_PATH)/include/

TEST_SRC = $(wildcard tests/cpp/*_test.cc)
TEST = $(patsubst tests/cpp/%_test.cc, tests/cpp/%_test, $(TEST_SRC))

tests/cpp/%_test: tests/cpp/%_test.cc lib/libnngraph.a
$(CXX) -std=c++11 $(CFLAGS) -MM -MT tests/cpp/$* $< >tests/cpp/$*.d
$(CXX) -std=c++11 $(CFLAGS) -I$(GTEST_INC) -o $@ $(filter %.cc %.a, $^) \
-L$(GTEST_LIB) $(LDFLAGS) -lgtest -lgtest_main

-include tests/cpp/*.d

0 comments on commit e858d65

Please sign in to comment.