summaryrefslogtreecommitdiff
path: root/ucoo/base/test
diff options
context:
space:
mode:
authorNicolas Schodet2015-06-03 13:46:44 +0200
committerNicolas Schodet2019-10-07 00:44:44 +0200
commit574480980dcffbfa7cd6dbe71e88253eb4c9feba (patch)
tree5f1c1261341a7c2a56de01d8c6d0815b164be8ba /ucoo/base/test
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/base/test')
-rw-r--r--ucoo/base/test/Config5
-rw-r--r--ucoo/base/test/Module1
-rw-r--r--ucoo/base/test/test.cc118
-rw-r--r--ucoo/base/test/test.hh99
-rw-r--r--ucoo/base/test/test.host.cc42
-rw-r--r--ucoo/base/test/test.stm32.cc48
-rw-r--r--ucoo/base/test/test/Makefile9
-rw-r--r--ucoo/base/test/test/test_test.cc48
8 files changed, 370 insertions, 0 deletions
diff --git a/ucoo/base/test/Config b/ucoo/base/test/Config
new file mode 100644
index 0000000..133808b
--- /dev/null
+++ b/ucoo/base/test/Config
@@ -0,0 +1,5 @@
+[base/test]
+wait = false
+
+[base/test:stm32]
+wait = true
diff --git a/ucoo/base/test/Module b/ucoo/base/test/Module
new file mode 100644
index 0000000..9a3c77b
--- /dev/null
+++ b/ucoo/base/test/Module
@@ -0,0 +1 @@
+base_test_SOURCES := test.cc test.host.cc test.stm32.cc
diff --git a/ucoo/base/test/test.cc b/ucoo/base/test/test.cc
new file mode 100644
index 0000000..4f2e8f0
--- /dev/null
+++ b/ucoo/base/test/test.cc
@@ -0,0 +1,118 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/common.hh"
+#include "ucoo/base/stdio/stdio.hh"
+#include "ucoo/utils/delay.hh"
+
+#include "config/base/test.hh"
+
+#include <cstdarg>
+
+namespace ucoo {
+
+TestSuite::TestSuite (const char *test_suite)
+ : test_suite_ (test_suite), test_group_ ("none"),
+ test_nb_ (0), fail_nb_ (0), in_test_ (false)
+{
+ 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
+TestSuite::report ()
+{
+ assert (!in_test_);
+ printf ("test results: %d tests, %d fails\n", test_nb_, fail_nb_);
+ return fail_nb_ == 0;
+}
+
+void
+TestSuite::group (const char *test_group)
+{
+ test_group_ = test_group;
+}
+
+Test::Test (TestSuite &suite, const char *test)
+ : suite_ (suite), test_ (test), failed_ (false)
+{
+ assert (!suite_.in_test_);
+ suite_.in_test_ = true;
+ suite_.test_nb_++;
+}
+
+Test::~Test (void)
+{
+ if (!failed_)
+ printf ("%s:%s:%s:P: pass\n", suite_.test_suite_, suite_.test_group_,
+ test_);
+ suite_.in_test_ = false;
+}
+
+void
+Test::fail ()
+{
+ fail ("fail");
+}
+
+void
+Test::fail (const char *fail_fmt, ...)
+{
+ va_list ap;
+ assert (!failed_);
+ printf ("%s:%s:%s:F: ", suite_.test_suite_, suite_.test_group_, test_);
+ va_start (ap, fail_fmt);
+ vprintf (fail_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+ suite_.fail_nb_++;
+ failed_ = true;
+}
+
+void
+Test::info (const char *info_fmt, ...)
+{
+ va_list ap;
+ printf ("%s:%s:%s:I: ", suite_.test_suite_, suite_.test_group_, test_);
+ va_start (ap, info_fmt);
+ vprintf (info_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/test/test.hh b/ucoo/base/test/test.hh
new file mode 100644
index 0000000..fd8ac32
--- /dev/null
+++ b/ucoo/base/test/test.hh
@@ -0,0 +1,99 @@
+#ifndef ucoo_base_test_test_hh
+#define ucoo_base_test_test_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/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 suite context.
+class TestSuite
+{
+ public:
+ /// Create a new test suite.
+ TestSuite (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);
+ private:
+ friend class Test;
+ /// Test suite name.
+ const char *test_suite_;
+ /// Test group name.
+ const char *test_group_;
+ /// Number of attempted tests.
+ int test_nb_;
+ /// Number of test failures.
+ int fail_nb_;
+ /// Running a test.
+ bool in_test_;
+};
+
+/// Test context.
+class Test
+{
+ public:
+ /// Start a new test, may be followed by fail, test stops at object
+ /// destruction.
+ Test (TestSuite &suite, const char *test);
+ /// Stop a test.
+ ~Test (void);
+ /// 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:
+ /// Attached test suite.
+ TestSuite &suite_;
+ /// Test name.
+ const char *test_;
+ /// Failed yet.
+ bool failed_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_base_test_test_hh
diff --git a/ucoo/base/test/test.host.cc b/ucoo/base/test/test.host.cc
new file mode 100644
index 0000000..d43d235
--- /dev/null
+++ b/ucoo/base/test/test.host.cc
@@ -0,0 +1,42 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/arch/host/host_stream.hh"
+
+namespace ucoo {
+
+Stream &
+test_stream (void)
+{
+ static HostStream s;
+ return s;
+}
+
+void
+test_stream_setup (void)
+{
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/test/test.stm32.cc b/ucoo/base/test/test.stm32.cc
new file mode 100644
index 0000000..3961a70
--- /dev/null
+++ b/ucoo/base/test/test.stm32.cc
@@ -0,0 +1,48 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/hal/usb/usb.hh"
+#include "ucoo/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/ucoo/base/test/test/Makefile b/ucoo/base/test/test/Makefile
new file mode 100644
index 0000000..610c71a
--- /dev/null
+++ b/ucoo/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/ucoo/base/test/test/test_test.cc b/ucoo/base/test/test/test_test.cc
new file mode 100644
index 0000000..dcc6b76
--- /dev/null
+++ b/ucoo/base/test/test/test_test.cc
@@ -0,0 +1,48 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/base/test/test.hh"
+#include "ucoo/arch/arch.hh"
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite test_suite ("test_test");
+ // Really, what a dumb test!
+ test_suite.group ("group one");
+ {
+ ucoo::Test test (test_suite, "the first test");
+ test.fail ("Oh no! test %d!", 123);
+ }
+ {
+ ucoo::Test test (test_suite, "try again");
+ test.info ("working harder...");
+ }
+ test_suite.group ("group two");
+ {
+ ucoo::Test test (test_suite, "simple test");
+ }
+ return test_suite.report () ? 0 : 1;
+}
+