Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from ithinuel/mock_coverage
Browse files Browse the repository at this point in the history
Rework tests to cover 100% of production code.
  • Loading branch information
ithinuel committed Sep 17, 2014
2 parents 3a00210 + 926e9cc commit 0d343e6
Show file tree
Hide file tree
Showing 37 changed files with 1,123 additions and 261 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
.project
.cproject
.settings

# autosave
*~
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "third_party/unity"]
path = third_party/unity
url = git://github.com/ThrowTheSwitch/Unity.git
url = git://github.com/ithinuel/Unity.git
[submodule "third_party/lwip"]
path = third_party/lwip
url = git://git.savannah.nongnu.org/lwip.git
2 changes: 1 addition & 1 deletion API/cexcept/cexcept.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
struct cexcept
{
const char * type;
const char * message;
char * message;
bool is_dynamic;

};
Expand Down
43 changes: 43 additions & 0 deletions API/collections/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2014 Chauveau Wilfried
Licensed 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 __COLLECTIONS_LIST_H__
#define __COLLECTIONS_LIST_H__
/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>
#include "common/object.h"

/* Types ---------------------------------------------------------------------*/
typedef struct
{
object_t base;
} list_t;

typedef struct _list_node_t list_node_t;

struct _list_node_t
{
list_t *owner;
list_node_t *prev;
list_node_t *next;
};

list_t * list_create (void);
bool list_push_back (list_t *this,
list_node_t *item);
list_node_t * list_pop_front (list_t *this);

#endif
2 changes: 1 addition & 1 deletion API/common/cexcept.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef struct cexcept_ctx cexcept_ctx_t;
* @param is_dynamic true if message should be clean.
*/
void cexcept_throw (const char *type,
const char *message,
char *message,
bool is_dynamic);

/**
Expand Down
4 changes: 3 additions & 1 deletion API/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include <stdbool.h>

/* Macros --------------------------------------------------------------------*/
#define container_of(ptr, type, member) \
(type *)((uintptr_t)ptr - __builtin_offsetof(type, member))
#define base_of(ptr, type) \
(type *)((uintptr_t)ptr - __builtin_offsetof(type, base))
container_of(ptr, type, base)

/* Public functions ----------------------------------------------------------*/
/**
Expand Down
5 changes: 3 additions & 2 deletions API/os/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/* Public types --------------------------------------------------------------*/
typedef void (*task_delay_ms_f) (int32_t ms);
typedef void (*task_start_f) (void *arg);
typedef void (*task_method_f) (void *arg);
typedef struct
{
object_t base;
Expand All @@ -51,13 +51,14 @@ void task_cexcept_set_ctx (cexcept_ctx_t *);
* @param priority Task priority.
* @return Create task or NULL.
*/
task_t * task_create (task_start_f routine,
task_t * task_create (task_method_f routine,
void *arg,
uint32_t stack_size,
uint32_t priority,
char *name);
bool task_start (task_t *this);
void task_stop (task_t *this);
bool task_must_stop (task_t *this);
uint32_t task_running_count (void);

#endif
4 changes: 0 additions & 4 deletions API/tests/common_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
#include <setjmp.h>
#include <stdint.h>

#define VERIFY_DIE_START \
if (setjmp(g_on_die)==0) {
#define VERIFY_DIE_END \
}

/* Public functions ----------------------------------------------------------*/
void die_Expect (char *expected_cause);
Expand Down
1 change: 1 addition & 0 deletions API/tests/memmgr_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void mock_memmgr_setup (void);
void mock_mm_alloc_Expect (uint32_t size);
void mock_mm_alloc_ExpectAndReturn (uint32_t size,
void *ret);
void mock_mm_alloc_IgnoreAndReturn (void *ret);
void mock_mm_free_Expect (void *ptr);
void mock_memmgr_verify (void);

Expand Down
4 changes: 2 additions & 2 deletions API/tests/memmgr_unity.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
limitations under the License.
*/

#ifndef __TESTS_MEMMGR_MOCK_H__
#define __TESTS_MEMMGR_MOCK_H__
#ifndef __TESTS_MEMMGR_UNITY_H__
#define __TESTS_MEMMGR_UNITY_H__

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
Expand Down
27 changes: 27 additions & 0 deletions API/utils/cstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2014 Chauveau Wilfried
Licensed 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 __UTILS_STRING_H__
#define __UTILS_STRING_H__

/**
* Duplicate the string.
* @param str String to duplicate.
* @return new string or NULL.
*/
char * cstring_dup (const char *str);

#endif
42 changes: 22 additions & 20 deletions boards/unity/cexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,28 @@ struct cexcept_ctx
uint8_t state;

cexcept_t excpt;
bool excpt_is_set;
bool is_set;
bool is_caught;
};

/* Public functions ----------------------------------------------------------*/
void cexcept_throw(const char *type, const char *message, bool is_dynamic)
void cexcept_throw(const char *type, char *message, bool is_dynamic)
{
cexcept_ctx_t *cur = task_cexcept_get_ctx();

if (cur == NULL) {
die("Throw without context");
}

if (cur->excpt_is_set && cur->excpt.is_dynamic) {
mm_free((void *)cur->excpt.message);
if (cur->is_set && cur->excpt.is_dynamic) {
mm_free(cur->excpt.message);
}

cur->excpt.type = type;
cur->excpt.message = message;
cur->excpt.is_dynamic = false;
cur->excpt_is_set = true;
cur->excpt.is_dynamic = is_dynamic;
cur->is_set = true;
cur->is_caught = false;
longjmp(cur->jmpbuf, cur->state);
}

Expand All @@ -62,7 +65,8 @@ void *cexcept_enter_ctx(void)
cexcept_throw("NOMEM", "no memory available to enter cexcept ctx.", false);
}
new->prev = task_cexcept_get_ctx();
new->excpt_is_set = false;
new->is_set = false;
new->is_caught = false;
new->state = 1;
task_cexcept_set_ctx(new);

Expand All @@ -76,7 +80,7 @@ cexcept_t *cexcept_catch(void)
die("Catch without context");
}
ctx->state = 2;
ctx->excpt_is_set = false;
ctx->is_caught = true;
return &ctx->excpt;
}

Expand All @@ -91,21 +95,19 @@ void cexcept_finally(void)

void cexcept_exit_ctx(void)
{
bool excpt_is_set = false;
cexcept_t ex = {0};
cexcept_ctx_t *cur = task_cexcept_get_ctx(),
*prev = NULL;
cexcept_ctx_t ctx = {0};
cexcept_ctx_t *cur = task_cexcept_get_ctx();
if (cur == NULL) {
die("Exit without context");
die("EndTry without context");
}

excpt_is_set = cur->excpt_is_set;
ex = cur->excpt;
prev = cur->prev;
task_cexcept_set_ctx(prev);

ctx = *cur;
mm_free(cur);
if (excpt_is_set) {
cexcept_throw(ex.type, ex.message, ex.is_dynamic);
task_cexcept_set_ctx(ctx.prev);

if (ctx.is_set && !ctx.is_caught) {
cexcept_throw(ctx.excpt.type, ctx.excpt.message, ctx.excpt.is_dynamic);
} else if (ctx.excpt.is_dynamic && (ctx.excpt.message != NULL)) {
mm_free(ctx.excpt.message);
}
}
25 changes: 1 addition & 24 deletions boards/unity/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,9 @@
#include "os/system.h"
#include "mcp/mcp.h"

jmp_buf g_on_die;
static char *gs_expected_cause = NULL;

void die(const char *reason)
{
if (gs_expected_cause != NULL) {
TEST_ASSERT_EQUAL_STRING(gs_expected_cause, reason);
gs_expected_cause= NULL;
longjmp(g_on_die, 1);
} else {
TEST_FAIL_MESSAGE(reason);
/* test fail may return on teardown */
exit(1);
}
}

void die_Expect(char *expected_cause)
{
gs_expected_cause = expected_cause;
}

void die_Verify(void)
{
if (gs_expected_cause != NULL) {
TEST_FAIL_MESSAGE("This test should have died");
}
TEST_FAIL_MESSAGE(reason);
}

int main(int argc, char **argv, char **arge)
Expand Down
Loading

0 comments on commit 0d343e6

Please sign in to comment.