summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/base/test
diff options
context:
space:
mode:
authorNicolas Schodet2012-12-23 15:25:12 +0100
committerNicolas Schodet2012-12-23 23:04:00 +0100
commit3e923e67e215a9de7a2078ed4d590828d0790f3b (patch)
treea759425124889a7b9cf7d59da9911b3e53062d7c /digital/ucoolib/ucoolib/base/test
parent7a6db77f598f0f4723b3f6048e372daa92ec2430 (diff)
digital/ucoolib/ucoolib/base/test: new test module
Diffstat (limited to 'digital/ucoolib/ucoolib/base/test')
-rw-r--r--digital/ucoolib/ucoolib/base/test/Config5
-rw-r--r--digital/ucoolib/ucoolib/base/test/Module1
-rw-r--r--digital/ucoolib/ucoolib/base/test/test.cc119
-rw-r--r--digital/ucoolib/ucoolib/base/test/test.hh84
-rw-r--r--digital/ucoolib/ucoolib/base/test/test.host.cc42
-rw-r--r--digital/ucoolib/ucoolib/base/test/test.stm32.cc48
-rw-r--r--digital/ucoolib/ucoolib/base/test/test/Makefile9
-rw-r--r--digital/ucoolib/ucoolib/base/test/test/test_test.cc44
8 files changed, 352 insertions, 0 deletions
diff --git a/digital/ucoolib/ucoolib/base/test/Config b/digital/ucoolib/ucoolib/base/test/Config
new file mode 100644
index 00000000..133808bd
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/Config
@@ -0,0 +1,5 @@
+[base/test]
+wait = false
+
+[base/test:stm32]
+wait = true
diff --git a/digital/ucoolib/ucoolib/base/test/Module b/digital/ucoolib/ucoolib/base/test/Module
new file mode 100644
index 00000000..9a3c77b7
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/Module
@@ -0,0 +1 @@
+base_test_SOURCES := test.cc test.host.cc test.stm32.cc
diff --git a/digital/ucoolib/ucoolib/base/test/test.cc b/digital/ucoolib/ucoolib/base/test/test.cc
new file mode 100644
index 00000000..4a52929e
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test.cc
@@ -0,0 +1,119 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program 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 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoolib/common.hh"
+#include "ucoolib/base/stdio/stdio.hh"
+#include "ucoolib/utils/delay.hh"
+
+#include "config/base/test.hh"
+
+#include <cstdarg>
+
+namespace ucoo {
+
+Test::Test (const char *test_suite)
+ : test_suite_ (test_suite), test_group_ ("none"), test_ (0),
+ test_nb_ (0), fail_nb_ (0)
+{
+ test_stream_setup ();
+ if (UCOO_CONFIG_BASE_TEST_WAIT)
+ {
+ Stream &s = test_stream ();
+ int n;
+ char b;
+ s.block (false);
+ do
+ {
+ printf ("waiting to start...\n");
+ delay (1);
+ n = s.read (&b, 1);
+ } while (n <= 0);
+ s.block ();
+ }
+}
+
+bool
+Test::report ()
+{
+ assert (!test_);
+ printf ("test results: %d tests, %d fails\n", test_nb_, fail_nb_);
+ return fail_nb_ == 0;
+}
+
+void
+Test::group (const char *test_group)
+{
+ test_group_ = test_group;
+}
+
+void
+Test::begin (const char *test)
+{
+ assert (!test_);
+ test_ = test;
+ test_nb_++;
+}
+
+void
+Test::pass ()
+{
+ assert (test_);
+ printf ("%s:%s:%s:P: pass\n", test_suite_, test_group_, test_);
+ test_ = 0;
+}
+
+void
+Test::fail ()
+{
+ fail ("fail");
+}
+
+void
+Test::fail (const char *fail_fmt, ...)
+{
+ va_list ap;
+ assert (test_);
+ printf ("%s:%s:%s:F: ", test_suite_, test_group_, test_);
+ va_start (ap, fail_fmt);
+ vprintf (fail_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+ fail_nb_++;
+ test_ = 0;
+}
+
+void
+Test::info (const char *info_fmt, ...)
+{
+ va_list ap;
+ assert (test_);
+ printf ("%s:%s:%s:I: ", test_suite_, test_group_, test_);
+ va_start (ap, info_fmt);
+ vprintf (info_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/base/test/test.hh b/digital/ucoolib/ucoolib/base/test/test.hh
new file mode 100644
index 00000000..6d8a613d
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test.hh
@@ -0,0 +1,84 @@
+#ifndef ucoolib_base_test_test_hh
+#define ucoolib_base_test_test_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program 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 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "ucoolib/intf/stream.hh"
+
+/// Test helpers to fail and break to end of current block.
+#define test_fail_break_unless(test, cond) \
+ ({ if (!(cond)) { \
+ test.fail (#cond); \
+ break; \
+ } })
+
+namespace ucoo {
+
+/// Get a reference to test stream. This stream is the preferred way to
+/// interact with a tester.
+Stream &
+test_stream (void);
+
+/// Setup stdin and stdout to use the reference test stream. On host, this
+/// function does nothing.
+void
+test_stream_setup (void);
+
+/// Test context.
+class Test
+{
+ public:
+ /// Create a new test suite.
+ Test (const char *test_suite);
+ /// Report the overall test results, return true if all tests passed.
+ bool report ();
+ /// Enter a new test group.
+ void group (const char *test_group);
+ /// Start a new test, should be followed by pass or fail.
+ void begin (const char *test);
+ /// End the current test with a pass status.
+ void pass ();
+ /// End the current test with a fail status.
+ void fail ();
+ /// End the current test with a fail status and message.
+ void __attribute__ ((format (printf, 2, 3)))
+ fail (const char *fail_fmt, ...);
+ /// Give info about the current test.
+ void __attribute__ ((format (printf, 2, 3)))
+ info (const char *info_fmt, ...);
+ private:
+ /// Test suite name.
+ const char *test_suite_;
+ /// Test group name.
+ const char *test_group_;
+ /// Test name.
+ const char *test_;
+ /// Number of attempted tests.
+ int test_nb_;
+ /// Number of test failures.
+ int fail_nb_;
+};
+
+} // namespace ucoo
+
+#endif // ucoolib_base_test_test_hh
diff --git a/digital/ucoolib/ucoolib/base/test/test.host.cc b/digital/ucoolib/ucoolib/base/test/test.host.cc
new file mode 100644
index 00000000..3b28c92f
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test.host.cc
@@ -0,0 +1,42 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program 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 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoolib/arch/host_stream.hh"
+
+namespace ucoo {
+
+Stream &
+test_stream (void)
+{
+ static HostStream s;
+ return s;
+}
+
+void
+test_stream_setup (void)
+{
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/base/test/test.stm32.cc b/digital/ucoolib/ucoolib/base/test/test.stm32.cc
new file mode 100644
index 00000000..2faf0d4d
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test.stm32.cc
@@ -0,0 +1,48 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program 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 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoolib/hal/usb/usb.hh"
+#include "ucoolib/arch/syscalls.newlib.hh"
+
+namespace ucoo {
+
+Stream &
+test_stream (void)
+{
+ static UsbStreamControl usc ("APBTeam", "test");
+ static UsbStream us (usc, 0);
+ return us;
+}
+
+void
+test_stream_setup (void)
+{
+ Stream *s = &test_stream ();
+ syscalls_streams[0] = s;
+ syscalls_streams[1] = s;
+ syscalls_streams[2] = s;
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/base/test/test/Makefile b/digital/ucoolib/ucoolib/base/test/test/Makefile
new file mode 100644
index 00000000..610c71ad
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = host stm32f4
+PROGS = test_test
+test_test_SOURCES = test_test.cc
+
+MODULES = utils base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/digital/ucoolib/ucoolib/base/test/test/test_test.cc b/digital/ucoolib/ucoolib/base/test/test/test_test.cc
new file mode 100644
index 00000000..baf70a38
--- /dev/null
+++ b/digital/ucoolib/ucoolib/base/test/test/test_test.cc
@@ -0,0 +1,44 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program 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 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "ucoolib/base/test/test.hh"
+#include "ucoolib/arch/arch.hh"
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::Test test ("test_test");
+ // Really, what a dumb test!
+ test.group ("group one");
+ test.begin ("the first test");
+ test.fail ("Oh no! test %d!", 123);
+ test.begin ("try again");
+ test.info ("working harder...");
+ test.pass ();
+ test.group ("group two");
+ test.begin ("simple test");
+ test.pass ();
+ return test.report () ? 0 : 1;
+}
+