From 9e6d543609fcf268b949da4180d8acb4177174d0 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Tue, 26 Mar 2019 18:46:20 +0100
Subject: [PATCH] Added a python test

---
 adaptors/python/Makefile    |  4 ++--
 plugins/libtest/libtest.c   |  2 +-
 test/Makefile               | 16 ++++++++++++----
 test/test_moberg4simulink.c |  1 +
 test/test_py.py             | 19 +++++++++++++++++++
 5 files changed, 35 insertions(+), 7 deletions(-)
 create mode 100644 test/test_py.py

diff --git a/adaptors/python/Makefile b/adaptors/python/Makefile
index 1085417..9f966ec 100644
--- a/adaptors/python/Makefile
+++ b/adaptors/python/Makefile
@@ -1,8 +1,8 @@
 MOBERG_VERSION=$(shell git describe --tags)
 
-all: BUILD
+all: BUILD INSTALL
 
-.PHONY: BUILD
+.PHONY: BUILD 
 BUILD:
 	python2 ./setup.py build
 	python3 ./setup.py build
diff --git a/plugins/libtest/libtest.c b/plugins/libtest/libtest.c
index eb202de..c0b6f2c 100644
--- a/plugins/libtest/libtest.c
+++ b/plugins/libtest/libtest.c
@@ -141,7 +141,7 @@ static struct moberg_status encoder_in_read(
   
   struct moberg_channel_context *channel = &encoder_in->channel_context;
   struct moberg_device_context *device = channel->device;
-  *value = device->digital;
+  *value = device->digital * (channel->index + 1);
   return MOBERG_OK;
 err_einval:
   return MOBERG_ERRNO(EINVAL);
diff --git a/test/Makefile b/test/Makefile
index ca31418..6f0426a 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,19 +1,27 @@
-TEST = test_start_stop test_io test_moberg4simulink
+CTEST = test_start_stop test_io test_moberg4simulink
+PYTEST=test_py
 CCFLAGS += -Wall -Werror -I$(shell pwd) -g
 LDFLAGS += -L$(shell pwd)/build/ -lmoberg
 ENV_TEST = LD_LIBRARY_PATH=../build XDG_CONFIG_HOME=.config XDG_CONFIG_DIRS=.
 LDFLAGS_test_moberg4simulink = -lmoberg4simulink
 CCFLAGS_test_moberg4simulink = -I../adaptors/matlab -Wall -Werror -I$(shell pwd) -g
-
+PYTHON2PATH=$(shell realpath ../adaptors/python/install/usr/lib*/python2*/site-packages)
+PYTHON3PATH=$(shell realpath ../adaptors/python/install/usr/lib*/python3*/site-packages)
 all:
 
 .PHONY: test
-test: $(TEST:%=run_%)
+test: $(PYTEST:%=run_py_%) $(CTEST:%=run_c_%) 
 	echo Tests run
 
-run_%:	build/%
+.PHONY: run_c_%
+run_c_%:build/%
 	$(ENV_TEST) valgrind --leak-check=full ./build/$*
 
+.PHONY: run_py_%
+run_py_%: %.py
+	$(ENV_TEST) PYTHONPATH=$(PYTHON2PATH) python2 $*.py
+	$(ENV_TEST) PYTHONPATH=$(PYTHON3PATH) python3 $*.py
+
 .PRECIOUS: build/%
 
 build/%: %.c | build
diff --git a/test/test_moberg4simulink.c b/test/test_moberg4simulink.c
index bfe8ee5..6dd5705 100644
--- a/test/test_moberg4simulink.c
+++ b/test/test_moberg4simulink.c
@@ -8,6 +8,7 @@ int main(int argc, char *argv[])
     goto out;
   }
   moberg4simulink_analog_in_close(0, ain);
+  return(0);
  out:
   return 1;
 }
diff --git a/test/test_py.py b/test/test_py.py
new file mode 100644
index 0000000..5753a1f
--- /dev/null
+++ b/test/test_py.py
@@ -0,0 +1,19 @@
+import moberg
+
+m = moberg.Moberg()
+
+ain = [ m.analog_in(i) for i in range(8) ]
+aout = [ m.analog_out(i) for i in range(8) ]
+din = [ m.digital_in(i) for i in range(8) ]
+dout = [ m.digital_out(i) for i in range(8) ]
+ein = [ m.encoder_in(i) for i in range(8) ]
+for v in range(10):
+    aout[0].write(v)
+    print([ c.read() for c in ain ])
+for v in range(10):
+    for c,w in zip(dout, range(100)):
+        dout[w].write(v & (1<<w))
+    print([ c.read() for c in din ])
+    print([ c.read() for c in ein ])
+    pass
+
-- 
GitLab