Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 6d5df7301 -> 257381d1c


MYNEWT-139: add testbench app for target unittests

New testbench app for running unit tests on target devices.
Clean up unit tests and infrastructure.
Refactor test code initialization so builtin selftest uses callbacks to be more 
consistent with testbench.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/257381d1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/257381d1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/257381d1

Branch: refs/heads/develop
Commit: 257381d1cf9affa8b5b25609c5181b9b9fcc4dda
Parents: 6d5df73
Author: Peter Snyder <[email protected]>
Authored: Tue Jan 3 19:01:13 2017 -0800
Committer: Peter Snyder <[email protected]>
Committed: Tue Jan 3 19:01:13 2017 -0800

----------------------------------------------------------------------
 apps/testbench/pkg.yml                          |  57 +++
 apps/testbench/src/testbench.c                  | 428 +++++++++++++++++++
 apps/testbench/src/testbench.h                  | 182 ++++++++
 apps/testbench/src/testbench_json.c             |  82 ++++
 apps/testbench/src/testbench_mempool.c          |  98 +++++
 apps/testbench/src/testbench_mutex.c            | 172 ++++++++
 apps/testbench/src/testbench_sem.c              | 141 ++++++
 apps/testbench/syscfg.yml                       |  33 ++
 kernel/os/test/src/mutex_test.c                 |  45 +-
 kernel/os/test/src/os_test.c                    |  27 +-
 kernel/os/test/src/sem_test.c                   |  41 +-
 .../os/test/src/testcases/os_mutex_test_basic.c |   5 -
 .../test/src/testcases/os_mutex_test_case_1.c   |   4 -
 .../test/src/testcases/os_mutex_test_case_2.c   |   8 -
 .../os/test/src/testcases/os_sem_test_basic.c   |   4 -
 .../os/test/src/testcases/os_sem_test_case_1.c  |   4 -
 .../os/test/src/testcases/os_sem_test_case_2.c  |   4 -
 .../os/test/src/testcases/os_sem_test_case_3.c  |   4 -
 .../os/test/src/testcases/os_sem_test_case_4.c  |   4 -
 test/testutil/include/testutil/testutil.h       |  26 +-
 test/testutil/src/case.c                        |  30 +-
 test/testutil/src/testutil.c                    |   5 +-
 22 files changed, 1292 insertions(+), 112 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/testbench/pkg.yml b/apps/testbench/pkg.yml
new file mode 100644
index 0000000..4e16304
--- /dev/null
+++ b/apps/testbench/pkg.yml
@@ -0,0 +1,57 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: apps/testbench
+pkg.type: app
+pkg.description: target test suite for high-level mynewt OS functionality
+pkg.author: "Apache Mynewt <[email protected]>"
+pkg.homepage: "http://mynewt.apache.org/";
+pkg.keywords:
+
+pkg.deps:
+    - sys/console/full
+    - mgmt/imgmgr
+    - mgmt/newtmgr
+    - mgmt/newtmgr/transport/nmgr_shell
+    - kernel/os
+    - boot/bootutil
+    - encoding/json/test
+#    - sys/shell
+    - sys/config
+    - sys/id
+    - sys/log
+    - sys/stats
+    - sys/flash_map
+    - sys/flash_map/test
+    - test/testutil
+    - kernel/os/test
+    - encoding/json/test
+    - test/crash_test
+    - test/runtest
+
+pkg.deps.CONFIG_NFFS:
+    - fs/nffs
+    - fs/nffs/test
+
+pkg.deps.CONFIG_FCB:
+    - fs/fcb
+    - fs/fcb/test
+
+
+#   pkg.cflags: -DDEBUG_MALLOC

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench.c
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench.c b/apps/testbench/src/testbench.c
new file mode 100644
index 0000000..f563638
--- /dev/null
+++ b/apps/testbench/src/testbench.c
@@ -0,0 +1,428 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "sysflash/sysflash.h"
+
+#include <os/os.h>
+#include "bsp/bsp.h"
+#include <hal/hal_gpio.h>
+#include <hal/hal_flash.h>
+#include <console/console.h>
+#if MYNEWT_VAL(SHELL_TASK)
+#include <shell/shell.h>
+#endif
+#include <log/log.h>
+#include <stats/stats.h>
+#include <config/config.h>
+#include "flash_map/flash_map.h"
+#include <hal/hal_system.h>
+#if MYNEWT_VAL(SPLIT_LOADER)
+#include "split/split.h"
+#endif
+#include <newtmgr/newtmgr.h>
+#include <bootutil/image.h>
+#include <bootutil/bootutil.h>
+#include <imgmgr/imgmgr.h>
+#include <assert.h>
+#include <string.h>
+#include <json/json.h>
+#include <reboot/log_reboot.h>
+#include <os/os_time.h>
+#include <id/id.h>
+#include <os/os_eventq.h>
+
+#include "testutil/testutil.h"
+
+#if MYNEWT_VAL(CONFIG_NFFS)
+#include <fs/fs.h>
+#include <nffs/nffs.h>
+#include "nffs/nffs_test.h"
+#endif /* NFFS */
+
+#if MYNEWT_VAL(CONFIG_FCB)
+#include <fcb/fcb.h>
+/*#include "../fcb/fcb_test.h"*/
+#endif /* FCB */
+
+#include "os/os_test.h"
+#include "bootutil/bootutil_test.h"
+
+#include <stddef.h>
+#include <config/config_file.h>
+#include "mbedtls/mbedtls_test.h"
+
+#if MYNEWT_VAL(RUNTEST_CLI)
+#include "runtest/runtest.h"
+#endif
+
+#include "testbench.h"
+
+struct os_timeval tv;
+struct os_timezone tz;
+
+/* Init all tasks */
+volatile int tasks_initialized;
+int init_tasks(void);
+
+/* Test Task */
+#define TESTTASK_PRIO (1)
+#define TESTTASK_STACK_SIZE    OS_STACK_ALIGN(256)
+static struct os_task testtask;
+
+/* newtmgr Task */
+#define NMGRTASK_PRIO (2)
+#define NMGRTASK_STACK_SIZE    OS_STACK_ALIGN(768)
+static struct os_task nmgrtask;
+
+/* For LED toggling */
+int g_led_pin;
+
+os_stack_t *stack1;
+os_stack_t *stack2;
+os_stack_t *stack3;
+os_stack_t *stack4;
+
+/*
+ * Test log cbmem buf
+ */
+#define MAX_CBMEM_BUF 2048
+uint32_t *cbmem_buf;
+struct cbmem cbmem;
+
+struct log testlog;
+int total_tests;
+int total_fails;
+int blinky_blink;
+
+int forcefail; /* to optionally force a fail on a tests */
+
+#define BLINKY_DUTYCYCLE_SUCCESS 1
+#define BLINKY_DUTYCYCLE_FAIL 16
+
+char buildID[TESTBENCH_BUILDID_SZ];
+
+static struct os_eventq run_evq;
+
+struct runtest_evq_arg test_event_arg;
+char runtest_token[RUNTEST_REQ_SIZE];
+static int testbench_runtests(struct os_event *ev);
+static void testbench_test_complete();
+
+#if 0
+void
+testbench_ts_init(void *arg)
+{
+    return;
+}
+
+void
+testbench_ts_pretest(void* arg)
+{
+    return;
+}
+
+void
+testbench_ts_posttest(void* arg)
+{
+    return;
+}
+#endif
+
+void
+testbench_ts_pass(char *msg, int msg_len, void *arg)
+{
+    TESTBENCH_UPDATE_TOD;
+    LOG_INFO(&testlog, LOG_MODULE_TEST, "%s test case %s PASSED %s %s",
+             buildID, tu_case_name, msg, runtest_token);
+    return;
+}
+
+void
+testbench_ts_fail(char *msg, int msg_len, void *arg)
+{
+    TESTBENCH_UPDATE_TOD;
+    LOG_INFO(&testlog, LOG_MODULE_TEST, "%s test case %s FAILED %s %s",
+             buildID, tu_case_name, msg, runtest_token);
+    return;
+}
+
+#if 0
+void
+testbench_tc_pretest(void* arg)
+{
+    return;
+}
+
+void
+testbench_tc_postest(void* arg)
+{
+    return;
+}
+#endif
+
+void
+testbench_test_init()
+{
+    total_tests = 0;
+    total_fails = 0;
+    forcefail = 0;
+    blinky_blink = BLINKY_DUTYCYCLE_SUCCESS;
+
+    return;
+}
+
+static int
+testbench_runtests(struct os_event *ev)
+{
+    struct ts_suite *ts;
+    struct runtest_evq_arg *runtest_arg;
+    int run_all = 0;
+
+    /*
+     * testbench_runtests is normally initiated from newtmanager using the cli
+     */
+    testbench_test_init();
+    if (ev != NULL) {
+        runtest_arg = ev->ev_arg;
+
+        ts_config.ts_print_results = 0;
+        ts_config.ts_system_assert = 0;
+
+        /*
+         * The specified "token" is appended to the end of every log message
+         * that is level INFO and above (i.e. not log_debug log messages)
+         */
+        strcpy(runtest_token, runtest_arg->run_token);
+
+        /*
+         * In run_all set, each test suite is executed
+         */
+        run_all = (strcmp(runtest_arg->run_testname, "all") == 0);
+
+        /*
+         * If no testname was provided (e.g., in the case where mgmt protocol
+         * did not use the newtmgr application), make the default to run all
+         * the tests.
+         */
+        if (runtest_arg->run_testname[0] == '\0') {
+            run_all = 1;
+        }
+
+        /*
+         * go through entire list of registered test suites
+         */
+        SLIST_FOREACH(ts, &g_ts_suites, ts_next) {
+            if (run_all || !strcmp(runtest_arg->run_testname, ts->ts_name)) {
+                ts->ts_test();
+                total_tests += tu_case_idx;
+                total_fails += tu_case_failed;
+            }
+        }
+    } else {
+        /*
+         * run all tests if NULL event is passed as an argument (untested)
+         */
+        SLIST_FOREACH(ts, &g_ts_suites, ts_next) {
+            ts->ts_test();
+            total_tests += tu_case_idx;
+            total_fails += tu_case_failed;
+        }
+    }
+    testbench_test_complete();
+
+    return tu_any_failed;
+}
+
+/*
+ * Print results - ci gateway is checking this message syntax to
+ * determine success or failure
+ */
+static void
+testbench_test_complete()
+{
+    LOG_INFO(&testlog, LOG_MODULE_TEST,
+             "%s TESTBENCH TEST %s - Tests run:%d pass:%d fail:%d %s",
+             buildID,
+             (total_fails ? "FAILED" : "PASSED"),
+             total_tests,
+             (total_tests-total_fails),
+             total_fails,
+             runtest_token);
+    return;
+}
+
+/*
+ * Run the tests
+ * If any tests fail, blink the LED BLINKY_DUTYCYCLE_FAIL (16) times a second
+ */
+static void
+testtask_handler(void *arg)
+{
+    os_gettimeofday(&tv, &tz);
+
+    g_led_pin = LED_BLINK_PIN;
+    hal_gpio_init_out(g_led_pin, 1);
+
+    while (1) {
+        /*
+         * if any test fails, blinky the LED more rapidly to
+         * provide visual feedback from physical device.
+         */
+        if (total_fails) {
+            blinky_blink = BLINKY_DUTYCYCLE_FAIL;
+        }
+
+        while (1) {
+            /* Wait one second */
+            os_time_delay(OS_TICKS_PER_SEC / blinky_blink);
+
+            /* Toggle the LED */
+            hal_gpio_toggle(g_led_pin);
+        }
+    }
+}
+
+/*
+ * Event callbacks are automatically run and this is how
+ * testbench_runtests (and the subsequent test suites) are intiated
+ */
+static void
+nmgrtask_handler(void *arg)
+{
+    while (1) {
+        os_eventq_run(&run_evq);
+    }
+}
+
+/*
+ * init_tasks include test workers 
+ */
+int
+init_tasks(void)
+{
+    os_stack_t *teststack;
+
+    /*
+     * malloc the stacks for the testworker tasks
+     */
+    stack1 = malloc(sizeof(os_stack_t) * TASK1_STACK_SIZE);
+    assert(stack1);
+
+    stack2 = malloc(sizeof(os_stack_t) * TASK2_STACK_SIZE);
+    assert(stack2);
+
+    stack3 = malloc(sizeof(os_stack_t) * TASK3_STACK_SIZE);
+    assert(stack3);
+
+    stack4 = malloc(sizeof(os_stack_t) * TASK4_STACK_SIZE);
+    assert(stack4);
+
+    teststack = malloc(sizeof(os_stack_t) * NMGRTASK_STACK_SIZE);
+    assert(teststack);
+    os_task_init(&nmgrtask, "nmgrtask", nmgrtask_handler, NULL,
+                 NMGRTASK_PRIO, OS_WAIT_FOREVER, teststack,
+                 NMGRTASK_STACK_SIZE);
+
+    teststack = malloc(sizeof(os_stack_t) * TESTTASK_STACK_SIZE);
+    assert(teststack);
+    os_task_init(&testtask, "testtask", testtask_handler, NULL,
+                 TESTTASK_PRIO, OS_WAIT_FOREVER, teststack,
+                 TESTTASK_STACK_SIZE);
+
+    tasks_initialized = 1;
+    return 0;
+}
+
+/*
+ * buildID string is prepended to each log message.
+ * BUILD_TARGET and BUILD_ID are assume to be initialized by
+ * build infrastructure. testbench.h sets default values if not.
+ */
+void
+getBuildID()
+{
+    sprintf(buildID, "%s Build %s:", BUILD_TARGET, BUILD_ID);
+}
+
+/*
+ * Test suites must be declared before being referenced.
+ * Note that we're not actually declaring the test suites themselves,
+ * but rather their helper functions which initialize the appropriate
+ * callbacks before executing on target HW.
+ */
+TEST_SUITE_DECL(testbench_mempool);
+TEST_SUITE_DECL(testbench_mutex);
+TEST_SUITE_DECL(testbench_sem);
+TEST_SUITE_DECL(testbench_json);
+
+/*
+ * main()
+ * Keep this app simple, just run the tests and then report success or failure.
+ * Complexity is pushed down to the individual test suites and component test 
cases.
+ */
+int
+main(int argc, char **argv)
+{
+    int rc;
+
+    sysinit();
+
+    getBuildID();
+
+    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
+    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
+    log_register("testlog", &testlog, &log_cbmem_handler, &cbmem, 
LOG_SYSLEVEL);
+
+    /*
+     * Initialize eventq and designate it as the default.
+     */
+    os_eventq_init(&run_evq);
+    os_eventq_dflt_set(&run_evq);
+
+    log_reboot(hal_reset_cause());
+
+    conf_load();
+
+    rc = init_tasks();
+
+    /*
+     * Register the tests that can be run by lookup
+     * - each test is added to the ts_suites slist
+     */
+    TEST_SUITE_REGISTER(testbench_mempool);
+    TEST_SUITE_REGISTER(testbench_mutex);
+    TEST_SUITE_REGISTER(testbench_sem);
+    TEST_SUITE_REGISTER(testbench_json);
+
+    /*
+     * This sets the callback function for the events that are
+     * generated from newtmanager.
+     */
+    run_evcb_set((os_event_fn*) testbench_runtests);
+
+    testbench_test_init(); /* initialize globals include blink duty cycle */
+
+    os_start();
+
+    /* os start should never return. If it does, this should be an error */
+    assert(0);
+
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench.h
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench.h b/apps/testbench/src/testbench.h
new file mode 100644
index 0000000..85b6470
--- /dev/null
+++ b/apps/testbench/src/testbench.h
@@ -0,0 +1,182 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef _H_TESTBENCH
+#define _H_TESTBENCH
+
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "sysflash/sysflash.h"
+
+#include <os/os.h>
+#include <bsp/bsp.h>
+#include <hal/hal_gpio.h>
+#include <hal/hal_flash.h>
+#include <console/console.h>
+#if MYNEWT_VAL(SHELL_TASK)
+#include <shell/shell.h>
+#endif
+#include <log/log.h>
+#include <stats/stats.h>
+#include <config/config.h>
+#include "flash_map/flash_map.h"
+#include <hal/hal_system.h>
+#if MYNEWT_VAL(SPLIT_LOADER)
+#include "split/split.h"
+#endif
+#include <newtmgr/newtmgr.h>
+#include <bootutil/image.h>
+#include <bootutil/bootutil.h>
+#include <imgmgr/imgmgr.h>
+#include <assert.h>
+#include <string.h>
+#include <json/json.h>
+#include <reboot/log_reboot.h>
+#include <os/os_time.h>
+#include <id/id.h>
+
+#include "testutil/testutil.h"
+
+#if MYNEWT_VAL(CONFIG_NFFS)
+#include <nffs/nffs.h>
+#include "nffs/nffs_test.h"
+#endif
+
+#if MYNEWT_VAL(CONFIG_FCB)
+#include <fcb/fcb.h>
+#endif
+
+#include "os/os_test.h"
+#include "bootutil/bootutil_test.h"
+
+#include <stddef.h>
+#include <config/config_file.h>
+#include "mbedtls/mbedtls_test.h"
+
+#ifdef __cplusplus
+#extern "C" {
+#endif
+
+#ifndef IMGMGR_HASH_LEN
+#define IMGMGR_HASH_LEN 32
+#endif
+
+extern char image_id[IMGMGR_HASH_LEN * 2 + 1];
+
+/* For LED toggling */
+int g_led_pin;
+
+#if MYNEWT_VAL(CONFIG_NFFS)
+/*
+ * NFFS
+ */
+int testbench_nffs();
+extern struct nffs_area_desc *nffs_current_area_descs;
+extern struct log nffs_log; /* defined in the OS module */
+#endif
+
+#if 0 /* not ready for this yet */
+#if MYNEWT_VAL(CONFIG_FCB)
+static struct flash_area conf_fcb_area[NFFS_AREA_MAX + 1];
+
+static struct conf_fcb my_conf = {
+    .cf_fcb.f_magic = 0xc09f6e5e,
+    .cf_fcb.f_sectors = conf_fcb_area
+};
+#endif
+#endif
+
+/*
+ * Test log cbmem buf
+ */
+extern uint32_t *cbmem_buf;
+extern struct cbmem cbmem;
+
+extern struct log testlog;
+
+#define TESTBENCH_BUILDID_SZ 64
+extern char buildID[TESTBENCH_BUILDID_SZ];
+
+/*
+ * defaults if not specified
+ */
+#ifndef BUILD_ID
+#define BUILD_ID "1.2.3.4"
+#endif
+
+#ifndef BUILD_TARGET
+#define BUILD_TARGET    "ARDUINO_ZERO"
+#endif
+
+/*
+ * Pool of test worker tasks and matching stacks
+ */
+#ifndef TASK1_PRIO
+#define TASK1_PRIO (21) 
+struct os_task task1;
+#endif
+
+#ifndef TASK2_PRIO
+#define TASK2_PRIO (22) 
+struct os_task task2;
+#endif
+
+#ifndef TASK3_PRIO
+#define TASK3_PRIO (23) 
+struct os_task task3;
+#endif
+
+#ifndef TASK4_PRIO
+#define TASK4_PRIO (24) 
+struct os_task task4;
+#endif
+
+extern os_stack_t *stack1;
+#define TASK1_STACK_SIZE TESTTASK_STACK_SIZE
+
+extern os_stack_t *stack2;
+#define TASK2_STACK_SIZE TESTTASK_STACK_SIZE
+
+extern os_stack_t *stack3;
+#define TASK3_STACK_SIZE TESTTASK_STACK_SIZE
+
+extern os_stack_t *stack4;
+#define TASK4_STACK_SIZE TESTTASK_STACK_SIZE
+
+/*
+ * Generic routines for testsuite and testcase callbacks
+ */
+void testbench_ts_init(void *arg);
+void testbench_ts_pretest(void* arg);
+void testbench_ts_posttest(void* arg);
+void testbench_ts_pass(char *msg, int msg_len, void *arg);
+void testbench_ts_fail(char *msg, int msg_len, void *arg);
+
+void testbench_tc_pretest(void* arg);
+void testbench_tc_postest(void* arg);
+
+#define TESTBENCH_TOD_DELAY    1
+
+/* XXX hack to allow the idle task to run and update the TOD */
+#define TESTBENCH_UPDATE_TOD os_time_delay(TESTBENCH_TOD_DELAY)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _H_TESTBENCH */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench_json.c
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench_json.c 
b/apps/testbench/src/testbench_json.c
new file mode 100644
index 0000000..1d1cc8e
--- /dev/null
+++ b/apps/testbench/src/testbench_json.c
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+
+#include "testbench.h"
+
+#ifndef JSON_BIGBUF_SIZE
+#define JSON_BIGBUF_SIZE 192
+#endif
+
+extern char *bigbuf;
+
+void
+testbench_json_init(void *arg)
+{
+    /*
+     * Lorem ipsum dolor sit amet, consectetur adipiscing elit,
+     * sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+     */
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s testbench json_init", buildID);
+
+    tu_case_idx = 0;
+    tu_case_failed = 0;
+
+    bigbuf = os_malloc(JSON_BIGBUF_SIZE);
+
+    tu_suite_set_pass_cb(testbench_ts_pass, NULL);
+    tu_suite_set_fail_cb(testbench_ts_fail, NULL);
+
+    return;
+}
+
+void
+testbench_json_complete(void *arg)
+{
+    os_free((void*)bigbuf);
+}
+
+TEST_CASE_DECL(test_json_simple_encode);
+TEST_CASE_DECL(test_json_simple_decode);
+
+TEST_SUITE(testbench_json_suite)
+{
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST, "%s testbench_json", buildID);
+
+    tu_suite_set_init_cb(testbench_json_init, NULL);
+    tu_suite_set_complete_cb(testbench_json_complete, NULL);
+
+    test_json_simple_encode();
+    test_json_simple_decode();
+}
+
+int
+testbench_json()
+{
+    tu_suite_set_init_cb(testbench_json_init, NULL);
+    tu_suite_set_complete_cb(testbench_json_complete, NULL);
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST, "%s testbench_json", buildID);
+    testbench_json_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench_mempool.c
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench_mempool.c 
b/apps/testbench/src/testbench_mempool.c
new file mode 100644
index 0000000..64b5f79
--- /dev/null
+++ b/apps/testbench/src/testbench_mempool.c
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+
+#include "testbench.h"
+
+/* Limit max blocks for testing */
+#define MEMPOOL_TEST_MAX_BLOCKS     (128)
+
+/* Create a memory pool for testing */
+#ifndef MEM_BLOCK_SIZE
+#define MEM_BLOCK_SIZE  (80)
+#endif
+
+#ifndef MEM_MEM_BLOCKS
+#define NUM_MEM_BLOCKS  (10)
+#endif
+
+/* Test memory pool structure */
+struct os_mempool g_TstMempool;
+
+/* Test memory pool buffer */
+os_membuf_t *TstMembuf;
+extern uint32_t TstMembufSz;
+
+/* Array of block pointers. */
+void *block_array[MEMPOOL_TEST_MAX_BLOCKS];
+
+void
+testbench_mempool_init(void *arg)
+{
+    /*
+     * Lorem ipsum dolor sit amet, consectetur adipiscing elit,
+     * sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+     */
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s testbench mempool_init", buildID);
+
+    tu_case_idx = 0;
+    tu_case_failed = 0;
+
+    TstMembufSz = (sizeof(os_membuf_t) *
+                   OS_MEMPOOL_SIZE(NUM_MEM_BLOCKS, MEM_BLOCK_SIZE));
+    TstMembuf = os_malloc(TstMembufSz);
+
+    tu_suite_set_pass_cb(testbench_ts_pass, NULL);
+    tu_suite_set_fail_cb(testbench_ts_fail, NULL);
+
+    return;
+}
+
+void
+testbench_mempool_complete(void *arg)
+{
+    os_free((void*)TstMembuf);
+}
+
+TEST_CASE_DECL(os_mempool_test_case)
+
+TEST_SUITE(testbench_mempool_suite)
+{
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST, "%s testbench_mempool", buildID);
+
+    tu_suite_set_init_cb(testbench_mempool_init, NULL);
+    tu_suite_set_complete_cb(testbench_mempool_complete, NULL);
+
+    os_mempool_test_case();
+}
+
+int
+testbench_mempool()
+{
+    tu_suite_set_init_cb(testbench_mempool_init, NULL);
+    tu_suite_set_complete_cb(testbench_mempool_complete, NULL);
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST, "%s testbench_mempool", buildID);
+    testbench_mempool_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench_mutex.c
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench_mutex.c 
b/apps/testbench/src/testbench_mutex.c
new file mode 100644
index 0000000..85bf684
--- /dev/null
+++ b/apps/testbench/src/testbench_mutex.c
@@ -0,0 +1,172 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/time.h>
+#include "sysinit/sysinit.h"
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os/os_test.h"
+
+#include "testbench.h"
+
+#ifdef ARCH_sim
+#define MUTEX_TEST_STACK_SIZE   1024
+#else
+#define MUTEX_TEST_STACK_SIZE   256
+#endif
+
+struct os_mutex g_mutex1;
+struct os_mutex g_mutex2;
+
+volatile int g_mutex_test;
+
+void
+testbench_mutex_ts_init(void *arg)
+{
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s starting %s", buildID, tu_case_name);
+}
+
+/*
+ * "Suspend" the test worker tasks after each test completes.
+ * These tasks can be re-used for other tests, the stack also can be re-used.
+ */
+void
+testbench_mutex_tc_posttest(void* arg)
+{
+    os_error_t err;
+    int taskcount = (int) arg;
+
+    if (taskcount >= 1) {
+        err = os_task_suspend(&task1);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 2) {
+        err = os_task_suspend(&task2);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 3) {
+        err = os_task_suspend(&task3);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 4) {
+        err = os_task_suspend(&task4);
+        TEST_ASSERT(err == OS_OK);
+    }
+
+    return;
+}
+
+void
+testbench_mutex_ts_pass(char *msg, int msg_len, void *arg)
+{
+    LOG_INFO(&testlog, LOG_MODULE_TEST,
+              "%s test case %s PASSED %s", buildID, tu_case_name, msg);
+}
+    
+void
+testbench_mutex_ts_fail(char *msg, int msg_len, void *arg)
+{
+    LOG_INFO(&testlog, LOG_MODULE_TEST,
+             "%s test case %s FAILED %s", buildID, tu_case_name, msg);
+}
+
+/*
+ * "Suspend" the test worker tasks after each test completes.
+ * These tasks can be re-used for other tests, the stack also can be re-used.
+ */
+void
+testbench_mutex_posttest(void* arg)
+{
+    os_error_t err;
+    int taskcount = (int) arg;
+
+    if (taskcount >= 1) {
+        err = os_task_suspend(&task1);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 2) {
+        err = os_task_suspend(&task2);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 3) {
+        err = os_task_suspend(&task3);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 4) {
+        err = os_task_suspend(&task4);
+        TEST_ASSERT(err == OS_OK);
+    }
+
+    return;
+}
+
+/*
+ * callback run before testbench_mutex_suite starts
+ */
+void
+testbench_mutex_init(void *arg)
+{
+    tu_case_idx = 0;
+    tu_case_failed = 0;
+
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s testbench test_init", buildID);
+
+    tu_suite_set_pass_cb(testbench_mutex_ts_pass, NULL);
+    tu_suite_set_fail_cb(testbench_mutex_ts_fail, NULL);
+
+    return;
+}
+
+TEST_CASE_DECL(os_mutex_test_basic)
+TEST_CASE_DECL(os_mutex_test_case_1)
+TEST_CASE_DECL(os_mutex_test_case_2)
+
+TEST_SUITE(testbench_mutex_suite)
+{
+    int taskcount;
+
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s mutex_suite start", buildID);
+
+    taskcount = 1;
+    tu_case_set_post_cb(testbench_mutex_tc_posttest, (void*)taskcount);
+    os_mutex_test_basic();
+
+    taskcount = 3;
+    tu_case_set_post_cb(testbench_mutex_tc_posttest, (void*)taskcount);
+    os_mutex_test_case_1();
+
+    taskcount = 4;
+    tu_case_set_post_cb(testbench_mutex_tc_posttest, (void*)taskcount);
+    os_mutex_test_case_2();
+}
+
+int
+testbench_mutex()
+{
+    tu_suite_set_init_cb(testbench_mutex_init, NULL);
+
+    testbench_mutex_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/src/testbench_sem.c
----------------------------------------------------------------------
diff --git a/apps/testbench/src/testbench_sem.c 
b/apps/testbench/src/testbench_sem.c
new file mode 100644
index 0000000..5801a60
--- /dev/null
+++ b/apps/testbench/src/testbench_sem.c
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include "sysinit/sysinit.h"
+#include "testutil/testutil.h"
+#include "os/os.h"
+
+#include "testbench.h"
+
+#define SEM_TEST_STACK_SIZE     256
+
+struct os_sem g_sem1;
+
+void
+testbench_sem_ts_init(void *arg)
+{
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s starting %s", buildID, tu_case_name);
+}
+
+void sem_test_basic_handler(void* arg);
+
+void
+testbench_sem_posttest(void* arg)
+{
+    os_error_t err;
+    int taskcount = (int) arg;
+
+    if (taskcount >= 1) {
+        err = os_task_suspend(&task1);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 2) {
+        err = os_task_suspend(&task2);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 3) {
+        err = os_task_suspend(&task3);
+        TEST_ASSERT(err == OS_OK);
+    }
+    if (taskcount >= 4) {
+        err = os_task_suspend(&task4);
+        TEST_ASSERT(err == OS_OK);
+    }
+
+    return;
+}
+
+/*
+ * run before testbench_sem starts executing
+ */
+void
+testbench_sem_init(void *arg)
+{
+    tu_case_idx = 0;
+    tu_case_failed = 0;
+
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST, "%s testbench_sem suite init", 
buildID);
+
+    tu_suite_set_pass_cb(testbench_ts_pass, NULL);
+    tu_suite_set_fail_cb(testbench_ts_fail, NULL);
+
+    return;
+}
+
+TEST_CASE(os_sem_test_null)
+{
+    return;
+}
+
+TEST_CASE(os_sem_test_fail)
+{
+    extern int forcefail;
+
+    if (forcefail) {
+        TEST_ASSERT(0);
+    }
+    return;
+}
+
+TEST_CASE_DECL(os_sem_test_null)
+TEST_CASE_DECL(os_sem_test_fail)
+TEST_CASE_DECL(os_sem_test_basic)
+TEST_CASE_DECL(os_sem_test_case_1)
+TEST_CASE_DECL(os_sem_test_case_2)
+TEST_CASE_DECL(os_sem_test_case_3)
+TEST_CASE_DECL(os_sem_test_case_4)
+
+TEST_SUITE(testbench_sem_suite)
+{
+    int taskcount;
+
+    os_sem_test_null();
+
+    taskcount = 1;
+    tu_case_set_post_cb(testbench_sem_posttest, (void*)taskcount);
+    os_sem_test_basic();
+
+    taskcount = 3;
+    tu_case_set_post_cb(testbench_sem_posttest, (void*)taskcount);
+    os_sem_test_case_1();
+
+    taskcount = 4;
+    tu_case_set_post_cb(testbench_sem_posttest, (void*)taskcount);
+    os_sem_test_case_2();
+
+    tu_case_set_post_cb(testbench_sem_posttest, (void*)taskcount);
+    os_sem_test_case_3();
+
+    tu_case_set_post_cb(testbench_sem_posttest, (void*)taskcount);
+    os_sem_test_case_4();
+}
+
+void
+testbench_sem()
+{
+    tu_suite_set_init_cb(testbench_sem_init, NULL);
+    testbench_sem_suite();
+
+    LOG_DEBUG(&testlog, LOG_MODULE_TEST,
+             "%s testbench_sem suite complete", buildID);
+
+    return;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/apps/testbench/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/testbench/syscfg.yml b/apps/testbench/syscfg.yml
new file mode 100644
index 0000000..eadc7f3
--- /dev/null
+++ b/apps/testbench/syscfg.yml
@@ -0,0 +1,33 @@
+# Package: apps/testbench
+
+syscfg.vals:
+    # Enable the shell task.
+    SHELL_TASK: 1
+
+    # Include names for statistics.
+    STATS_NAMES: 1
+
+    # Log reboot messages to a flash circular buffer.
+    REBOOT_LOG_FCB: 1
+    LOG_FCB: 1
+
+    # Enable coredump
+    OS_COREDUMP: 1
+    IMGMGR_COREDUMP: 1
+
+#   CONFIG_NFFS: 1
+    CONFIG_FCB: 1
+
+    # Enable shell commands.
+    #    STATS_CLI: 1
+    LOG_CLI: 1
+    CONFIG_CLI: 1
+
+    # Enable newtmgr commands.
+    LOG_NEWTMGR: 1
+    CONFIG_NEWTMGR: 1
+
+    RUNTEST_CLI: 1
+    RUNTEST_NEWTMGR: 1
+
+#    BSP_ARDUINO_ZERO_PRO: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/mutex_test.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/mutex_test.c b/kernel/os/test/src/mutex_test.c
index 2cefa70..feef13c 100644
--- a/kernel/os/test/src/mutex_test.c
+++ b/kernel/os/test/src/mutex_test.c
@@ -42,6 +42,10 @@ os_stack_t *stack3;
 
 struct os_task task4;
 os_stack_t *stack4;
+
+struct os_mutex g_mutex1;
+struct os_mutex g_mutex2;
+volatile int g_mutex_test;
 #endif /* MYNEWT_VAL(SELFTEST) */
 
 volatile int g_task1_val;
@@ -49,12 +53,10 @@ volatile int g_task2_val;
 volatile int g_task3_val;
 volatile int g_task4_val;
 
-#if MYNEWT_VAL(SELFTEST)
-struct os_mutex g_mutex1;
-struct os_mutex g_mutex2;
-volatile int g_mutex_test;
-#endif
-
+/*
+ * Handlers for each of the test threads are implemented here as they
+ * are shared amongst multiple test cases.
+ */
 /**
  * mutex test basic 
  *  
@@ -327,20 +329,37 @@ mutex_task4_handler(void *arg)
 }
 
 void
-os_mutex_ts_pretest(void* arg)
+os_mutex_tc_pretest(void* arg)
 {
+#if MYNEWT_VAL(SELFTEST)
+    /*
+     * Only call if running in "native" simulated environment
+     */
     sysinit();
+#endif
+    return;
 }
 
 void
-os_mutex_ts_posttest(void* arg)
+os_mutex_tc_posttest(void* arg)
 {
+#if MYNEWT_VAL(SELFTEST)
+    /*
+     * Only call if running in "native" simulated environment
+     */
+    os_start();
+#endif
     return;
 }
 
 void
 os_mutex_test_init(void *arg)
 {
+
+    /*
+     * Stack should be allocated in target environemnt
+     * so they are sized correctly.
+     */
 #if MYNEWT_VAL(SELFTEST)
     stack1 = malloc(sizeof(os_stack_t) * MUTEX_TEST_STACK_SIZE);
     assert(stack1);
@@ -351,9 +370,6 @@ os_mutex_test_init(void *arg)
     stack4 = malloc(sizeof(os_stack_t) * MUTEX_TEST_STACK_SIZE);
     assert(stack4);
 #endif
-
-    tu_suite_set_pre_test_cb(os_mutex_ts_pretest, NULL);
-    tu_suite_set_post_test_cb(os_mutex_ts_posttest, NULL);
 }
 
 TEST_CASE_DECL(os_mutex_test_basic)
@@ -362,7 +378,14 @@ TEST_CASE_DECL(os_mutex_test_case_2)
 
 TEST_SUITE(os_mutex_test_suite)
 {
+    tu_case_set_post_cb(os_mutex_tc_posttest, NULL);
     os_mutex_test_basic();
+
+    tu_case_set_pre_cb(os_mutex_tc_pretest, NULL);
+    tu_case_set_post_cb(os_mutex_tc_posttest, NULL);
     os_mutex_test_case_1();
+
+    tu_case_set_pre_cb(os_mutex_tc_pretest, NULL);
+    tu_case_set_post_cb(os_mutex_tc_posttest, NULL);
     os_mutex_test_case_2();
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/os_test.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/os_test.c b/kernel/os/test/src/os_test.c
index 7b8fc09..7bb5028 100644
--- a/kernel/os/test/src/os_test.c
+++ b/kernel/os/test/src/os_test.c
@@ -30,8 +30,12 @@
 #include <sys/time.h>
 #include "os/os.h"
 
+/*
+ * Most of this file is the driver for the kernel selftest running in sim
+ * In the sim environment, we can initialize and restart mynewt at will
+ * where that is not the case when the test cases are run in a target env.
+ */
 #if MYNEWT_VAL(SELFTEST)
-
 void
 os_test_restart(void)
 {
@@ -57,6 +61,22 @@ os_test_restart(void)
    tu_restart();
 }
 
+/*
+ * sysinit and os_start are only called if running in a sim environment
+ * (ie MYNEWT_VAL(SELFTEST) is set)
+ */
+void
+os_selftest_pretest_cb(void* arg)
+{
+    sysinit();
+}
+
+void
+os_selftest_posttest_cb(void *arg)
+{
+    os_start();
+}
+
 extern void os_mempool_test_init(void *arg);
 extern void os_sem_test_init(void *arg);
 extern void os_mutex_test_init(void *arg);
@@ -96,11 +116,12 @@ main(int argc, char **argv)
 }
 
 #else
-
+/*
+ * Leave this as an implemented function for non-sim test environments
+ */
 void
 os_test_restart(void)
 {
     return;
 }
-
 #endif /* MYNEWT_VAL(SELFTEST) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/sem_test.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/sem_test.c b/kernel/os/test/src/sem_test.c
index acb042a..1daf49f 100644
--- a/kernel/os/test/src/sem_test.c
+++ b/kernel/os/test/src/sem_test.c
@@ -65,19 +65,10 @@ char sem_test_buf[128];
 const char *
 sem_test_sem_to_s(const struct os_sem *sem)
 {
-#if 0
-    char buf[128];
-
-    snprintf(buf, sizeof buf, "\tSemaphore: tokens=%u head=%p",
-             sem->sem_tokens, SLIST_FIRST(&sem->sem_head));
-
-    return buf;
-#else
     snprintf(sem_test_buf, sizeof sem_test_buf, "\tSemaphore: tokens=%u 
head=%p",
              sem->sem_tokens, SLIST_FIRST(&sem->sem_head));
 
     return sem_test_buf;
-#endif
 }
 
 void 
@@ -285,20 +276,30 @@ sem_test_4_task4_handler(void *arg)
 }
 
 void
-os_sem_ts_pretest(void* arg)
+os_sem_tc_pretest(void* arg)
 {
+#if MYNEWT_VAL(SELFTEST)
     sysinit();
+#endif
+    return;
 }
 
 void
-os_sem_ts_posttest(void* arg)
+os_sem_tc_posttest(void* arg)
 {
+#if MYNEWT_VAL(SELFTEST)
+    os_start();
+#endif
     return;
 }
 
 void
 os_sem_test_init(void *arg)
 {
+    /*
+     * Only allocate stacks here for selftest running in sim environment.
+     * Testing apps should allocate stacks for BSP environments
+     */
 #if MYNEWT_VAL(SELFTEST)
     stack1 = malloc(sizeof(os_stack_t) * SEM_TEST_STACK_SIZE);
     assert(stack1);
@@ -309,9 +310,6 @@ os_sem_test_init(void *arg)
     stack4 = malloc(sizeof(os_stack_t) * SEM_TEST_STACK_SIZE);
     assert(stack4);
 #endif
-
-    tu_suite_set_pre_test_cb(os_sem_ts_pretest, NULL);
-    tu_suite_set_post_test_cb(os_sem_ts_posttest, NULL);
 }
 
 TEST_CASE_DECL(os_sem_test_basic)
@@ -322,10 +320,23 @@ TEST_CASE_DECL(os_sem_test_case_4)
 
 TEST_SUITE(os_sem_test_suite)
 {
+    tu_case_set_pre_cb(os_sem_tc_pretest, NULL);
+    tu_case_set_post_cb(os_sem_tc_posttest, NULL);
     os_sem_test_basic();
+
+    tu_case_set_pre_cb(os_sem_tc_pretest, NULL);
+    tu_case_set_post_cb(os_sem_tc_posttest, NULL);
     os_sem_test_case_1();
+
+    tu_case_set_pre_cb(os_sem_tc_pretest, NULL);
+    tu_case_set_post_cb(os_sem_tc_posttest, NULL);
     os_sem_test_case_2();
+
+    tu_case_set_pre_cb(os_sem_tc_pretest, NULL);
+    tu_case_set_post_cb(os_sem_tc_posttest, NULL);
     os_sem_test_case_3();
+
+    tu_case_set_pre_cb(os_sem_tc_pretest, NULL);
+    tu_case_set_post_cb(os_sem_tc_posttest, NULL);
     os_sem_test_case_4();
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_mutex_test_basic.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_mutex_test_basic.c 
b/kernel/os/test/src/testcases/os_mutex_test_basic.c
index 2f76d89..9fa1d9d 100644
--- a/kernel/os/test/src/testcases/os_mutex_test_basic.c
+++ b/kernel/os/test/src/testcases/os_mutex_test_basic.c
@@ -20,13 +20,8 @@
 
 TEST_CASE(os_mutex_test_basic)
 {
-
     os_mutex_init(&g_mutex1);
 
     os_task_init(&task1, "task1", mutex_test_basic_handler, NULL,
                  TASK1_PRIO, OS_WAIT_FOREVER, stack1, sizeof(stack1));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_mutex_test_case_1.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_mutex_test_case_1.c 
b/kernel/os/test/src/testcases/os_mutex_test_case_1.c
index 505ee34..ee326e8 100644
--- a/kernel/os/test/src/testcases/os_mutex_test_case_1.c
+++ b/kernel/os/test/src/testcases/os_mutex_test_case_1.c
@@ -40,8 +40,4 @@ TEST_CASE(os_mutex_test_case_1)
 
     os_task_init(&task3, "task3", mutex_task3_handler, NULL, TASK3_PRIO, 
             OS_WAIT_FOREVER, stack3, sizeof(stack3));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_mutex_test_case_2.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_mutex_test_case_2.c 
b/kernel/os/test/src/testcases/os_mutex_test_case_2.c
index b284c8e..757dd4c 100644
--- a/kernel/os/test/src/testcases/os_mutex_test_case_2.c
+++ b/kernel/os/test/src/testcases/os_mutex_test_case_2.c
@@ -20,10 +20,6 @@
 
 TEST_CASE(os_mutex_test_case_2)
 {
-#if MYNEWT_VAL(SELFTEST)
-    sysinit();
-#endif
-
     g_mutex_test = 2;
     g_task1_val = 0;
     g_task2_val = 0;
@@ -42,8 +38,4 @@ TEST_CASE(os_mutex_test_case_2)
 
     os_task_init(&task4, "task4", mutex_task4_handler, NULL, TASK4_PRIO, 
             OS_WAIT_FOREVER, stack4, sizeof(stack4));
- 
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_sem_test_basic.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_sem_test_basic.c 
b/kernel/os/test/src/testcases/os_sem_test_basic.c
index 2cdc9a1..7715f7d 100644
--- a/kernel/os/test/src/testcases/os_sem_test_basic.c
+++ b/kernel/os/test/src/testcases/os_sem_test_basic.c
@@ -28,8 +28,4 @@ TEST_CASE(os_sem_test_basic)
     os_task_init(&task1, "task1", sem_test_basic_handler, NULL,
                  TASK1_PRIO, OS_WAIT_FOREVER, stack1,
                  sizeof(stack1));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_sem_test_case_1.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_sem_test_case_1.c 
b/kernel/os/test/src/testcases/os_sem_test_case_1.c
index 6d603a9..c736e99 100644
--- a/kernel/os/test/src/testcases/os_sem_test_case_1.c
+++ b/kernel/os/test/src/testcases/os_sem_test_case_1.c
@@ -36,8 +36,4 @@ TEST_CASE(os_sem_test_case_1)
     os_task_init(&task3, "task3", sem_test_1_task3_handler, NULL,
                  TASK3_PRIO, OS_WAIT_FOREVER, stack3,
                  sizeof(stack3));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_sem_test_case_2.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_sem_test_case_2.c 
b/kernel/os/test/src/testcases/os_sem_test_case_2.c
index e25f6ef..a816e06 100644
--- a/kernel/os/test/src/testcases/os_sem_test_case_2.c
+++ b/kernel/os/test/src/testcases/os_sem_test_case_2.c
@@ -40,8 +40,4 @@ TEST_CASE(os_sem_test_case_2)
     os_task_init(&task4, "task4", sem_test_2_task4_handler, NULL,
                  TASK4_PRIO, OS_WAIT_FOREVER, stack4,
                  sizeof(stack4));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_sem_test_case_3.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_sem_test_case_3.c 
b/kernel/os/test/src/testcases/os_sem_test_case_3.c
index 2dc3dbe..3b0a0ea 100644
--- a/kernel/os/test/src/testcases/os_sem_test_case_3.c
+++ b/kernel/os/test/src/testcases/os_sem_test_case_3.c
@@ -40,8 +40,4 @@ TEST_CASE(os_sem_test_case_3)
     os_task_init(&task4, "task4", sem_test_3_task4_handler, NULL,
                  TASK4_PRIO, OS_WAIT_FOREVER, stack4,
                  sizeof(stack4));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/kernel/os/test/src/testcases/os_sem_test_case_4.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/testcases/os_sem_test_case_4.c 
b/kernel/os/test/src/testcases/os_sem_test_case_4.c
index a9f32de..3a04a19 100644
--- a/kernel/os/test/src/testcases/os_sem_test_case_4.c
+++ b/kernel/os/test/src/testcases/os_sem_test_case_4.c
@@ -40,8 +40,4 @@ TEST_CASE(os_sem_test_case_4)
     os_task_init(&task4, "task4", sem_test_4_task4_handler, NULL,
                  TASK4_PRIO, OS_WAIT_FOREVER, stack4,
                  sizeof(stack4));
-
-#if MYNEWT_VAL(SELFTEST)
-    os_start();
-#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/test/testutil/include/testutil/testutil.h
----------------------------------------------------------------------
diff --git a/test/testutil/include/testutil/testutil.h 
b/test/testutil/include/testutil/testutil.h
index 3fa5f5f..1edfb03 100644
--- a/test/testutil/include/testutil/testutil.h
+++ b/test/testutil/include/testutil/testutil.h
@@ -51,9 +51,6 @@ extern "C" {
 typedef void tu_case_report_fn_t(char *msg, int msg_len, void *arg);
 typedef void tu_suite_restart_fn_t(void *arg);
 
-typedef void tu_pre_test_fn_t(void *arg);
-typedef void tu_post_test_fn_t(void *arg);
-
 typedef void tu_init_test_fn_t(void *arg);
 typedef void tu_pre_test_fn_t(void *arg);
 typedef void tu_post_test_fn_t(void *arg);
@@ -186,7 +183,6 @@ extern struct ts_config *ts_current_config;
 
 extern const char *tu_suite_name;
 extern const char *tu_case_name;
-extern int tu_first_idx;
 
 extern int tu_any_failed;
 extern int tu_suite_failed;
@@ -218,8 +214,8 @@ TEST_SUITE_##suite_name(void);                              
 \
     TEST_SUITE_##suite_name(void)
 
 /*
- * for creating multiple files with test cases
- * all belonging to the same suite
+ * For declaring the test cases across multiple files
+ * belonging to the same suite
  */
 #define TEST_CASE_DECL(case_name)                            \
     int case_name(void);
@@ -234,19 +230,17 @@ TEST_SUITE_##suite_name(void);                            
   \
     case_name(void)                                           \
     {                                                         \
         tu_suite_pre_test();                                  \
-        if (tu_case_idx >= tu_first_idx) {                    \
-            tu_case_init(#case_name);                         \
+        tu_case_init(#case_name);                             \
                                                               \
-            tu_case_pre_test();                               \
-            if (setjmp(tu_case_jb) == 0) {                    \
-                TEST_CASE_##case_name();                      \
-                tu_case_post_test();                          \
-                if (!tu_case_failed) {                        \
-                    tu_case_pass();                           \
-                }                                             \
+        tu_case_pre_test();                                   \
+        if (setjmp(tu_case_jb) == 0) {                        \
+            TEST_CASE_##case_name();                          \
+            tu_case_post_test();                              \
+            if (!tu_case_failed) {                            \
+                tu_case_pass();                               \
             }                                                 \
-            tu_case_complete();                               \
         }                                                     \
+        tu_case_complete();                                   \
         tu_suite_post_test();                                 \
                                                               \
         return tu_case_failed;                                \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/test/testutil/src/case.c
----------------------------------------------------------------------
diff --git a/test/testutil/src/case.c b/test/testutil/src/case.c
index d6de426..060f00c 100644
--- a/test/testutil/src/case.c
+++ b/test/testutil/src/case.c
@@ -31,7 +31,8 @@ int tu_case_idx;
 
 const char *tu_case_name;
 
-#define TU_CASE_BUF_SZ      1024
+/*#define TU_CASE_BUF_SZ      1024*/
+#define TU_CASE_BUF_SZ      256
 
 static char tu_case_buf[TU_CASE_BUF_SZ];
 static int tu_case_buf_len;
@@ -173,7 +174,6 @@ tu_case_pass(void)
     }
 }
 
-#if 0
 void
 tu_case_fail(void)
 {
@@ -196,30 +196,6 @@ tu_case_fail(void)
                                   ts_config.ts_case_fail_arg);
     }
 }
-#endif
-
-static void
-tu_case_write_fail_buf(void)
-{
-    tu_case_reported = 1;
-    tu_case_failed = 1;
-    tu_suite_failed = 1;
-    tu_any_failed = 1;
-
-#if MYNEWT_VAL(SELFTEST)
-    if (ts_config.ts_print_results) {
-        printf("[FAIL] %s/%s %s", ts_current_config->ts_suite_name, 
tu_case_name, tu_case_buf);
-        fflush(stdout);
-    }
-#endif
-
-    tu_case_post_test();
-
-    if (ts_config.ts_case_fail_cb != NULL) {
-        ts_config.ts_case_fail_cb(tu_case_buf, tu_case_buf_len,
-                                  ts_config.ts_case_fail_arg);
-    }
-}
 
 static void
 tu_case_append_file_info(const char *file, int line)
@@ -308,7 +284,7 @@ tu_case_fail_assert(int fatal, const char *file, int line,
     rc = tu_case_append_buf("\n");
     assert(rc == 0);
 
-    tu_case_write_fail_buf();
+    tu_case_fail();
 
     if (fatal) {
         tu_case_abort();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/257381d1/test/testutil/src/testutil.c
----------------------------------------------------------------------
diff --git a/test/testutil/src/testutil.c b/test/testutil/src/testutil.c
index 8a560db..6abb62b 100644
--- a/test/testutil/src/testutil.c
+++ b/test/testutil/src/testutil.c
@@ -34,14 +34,15 @@ struct ts_config ts_config;
 struct ts_config *ts_current_config = &ts_config;
 
 int tu_any_failed;
-int tu_first_idx;
 
 struct ts_testsuite_list *ts_suites;
 
 int
 tu_init(void)
 {
+#if MYNEWT_VAL(SELFTEST)
     sysinit();
+#endif
 
     return 0;
 }
@@ -81,8 +82,6 @@ tu_restart(void)
 {
     tu_case_write_pass_auto();
 
-    tu_first_idx = tu_case_idx + 1;
-
     if (ts_config.ts_restart_cb != NULL) {
         ts_config.ts_restart_cb(ts_config.ts_restart_arg);
     }

Reply via email to