Skip to content

Commit fa03368

Browse files
authored
Merge pull request #775 from ml-physec/more-details
Detail-Stack API
2 parents b9d897b + bfc785c commit fa03368

File tree

10 files changed

+433
-30
lines changed

10 files changed

+433
-30
lines changed

examples/example_5/makefile

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# =========================================================================
2+
# Unity - A Test Framework for C
3+
# ThrowTheSwitch.org
4+
# Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
5+
# SPDX-License-Identifier: MIT
6+
# =========================================================================
7+
8+
#We try to detect the OS we are running on, and adjust commands as needed
9+
ifeq ($(OS),Windows_NT)
10+
ifeq ($(shell uname -s),) # not in a bash-like shell
11+
CLEANUP = del /F /Q
12+
MKDIR = mkdir
13+
else # in a bash-like shell, like msys
14+
CLEANUP = rm -f
15+
MKDIR = mkdir -p
16+
endif
17+
TARGET_EXTENSION=.exe
18+
else
19+
CLEANUP = rm -f
20+
MKDIR = mkdir -p
21+
TARGET_EXTENSION=.out
22+
endif
23+
24+
C_COMPILER=gcc
25+
ifeq ($(shell uname -s), Darwin)
26+
C_COMPILER=clang
27+
endif
28+
29+
UNITY_ROOT=../..
30+
31+
CFLAGS=-std=c89
32+
CFLAGS += -Wall
33+
CFLAGS += -Wextra
34+
CFLAGS += -Wpointer-arith
35+
CFLAGS += -Wcast-align
36+
CFLAGS += -Wwrite-strings
37+
CFLAGS += -Wswitch-default
38+
CFLAGS += -Wunreachable-code
39+
CFLAGS += -Winit-self
40+
CFLAGS += -Wmissing-field-initializers
41+
CFLAGS += -Wno-unknown-pragmas
42+
CFLAGS += -Wstrict-prototypes
43+
CFLAGS += -Wundef
44+
CFLAGS += -Wold-style-definition
45+
#CFLAGS += -Wno-misleading-indentation
46+
47+
48+
TARGET_BASE1=test1
49+
TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION)
50+
SRC_FILES1=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c
51+
INC_DIRS=-Isrc -I$(UNITY_ROOT)/src
52+
SYMBOLS=-include"test/unity_detail_config.h" -DUNIT_TESTING
53+
54+
all: clean default
55+
56+
default: $(SRC_FILES1)
57+
$(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1)
58+
- ./$(TARGET1)
59+
clean:
60+
$(CLEANUP) $(TARGET1) $(TARGET2)
61+
62+
ci: CFLAGS += -Werror
63+
ci: default

examples/example_5/readme.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Example 5
2+
=========
3+
4+
Demonstrate Details Stack usage to implement something similar to a stacktrace.
5+
This allows locating the error source much faster in branching/iterating code.
6+
7+
Build and run with Make
8+
---
9+
Just run `make`.
10+
11+
Output
12+
---
13+
Below the output is annotated with source of the elements.
14+
15+
```
16+
test/TestProductionCode.c:36:test_BitExtractor:FAIL: Expected 0 Was 1. During call BitExtractor. During call BitExtractor_down. Bit Position 6. Bit Mask 0x02. Unexpected bit value
17+
```
18+
19+
| String | Source |
20+
|-----------------------------|---------------------------------------------------------------------------|
21+
| `test/TestProductionCode.c` | `Unity.TestFile` |
22+
| `36` | `UNITY_TEST_ASSERT_EQUAL_INT` line |
23+
| `test_BitExtractor` | `RUN_TEST` name |
24+
| `FAIL` | `UnityStrFail` |
25+
| `Expected 1 Was 0` | `UnityAssertEqualNumber` |
26+
| `During call` | Detail 0, Label |
27+
| `BitExtractor` | Detail 0, Value |
28+
| `During call` | Detail 0, Label |
29+
| `BitExtractor` | Detail 0, Value |
30+
| `During call` | Detail 1, Label |
31+
| `BitExtractor_down` | Detail 1, Value |
32+
| `Bit Position` | Detail 2, Label (literal starts with #\x18, so value is printed as INT32) |
33+
| `6` | Detail 2 Value |
34+
| `Bit Mask` | Detail 2, Label (literal starts with #\x41, so value is printed as HEX8) |
35+
| `0x02` | Detail 2 Value |
36+
| `Unexpected bit value` | `UNITY_TEST_ASSERT_EQUAL_INT` message |
37+
38+
While this example is a bit contrived, the source of the error can be clearly located to be within the `test_BitExtractor->BitExtractor->BitExtractor_down`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* =========================================================================
2+
Unity - A Test Framework for C
3+
ThrowTheSwitch.org
4+
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
5+
SPDX-License-Identifier: MIT
6+
========================================================================= */
7+
8+
#include "ProductionCode.h"
9+
10+
#include <stdint.h>
11+
12+
#ifdef UNIT_TESTING
13+
#include "unity.h"
14+
#else
15+
/* No-Op when not testing */
16+
#define UNITY_DETAIL_PUSH
17+
#define UNITY_DETAIL_POP
18+
#endif
19+
20+
static void BitExtractor_up(uint8_t input, callback_t cb)
21+
{
22+
int32_t pos;
23+
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
24+
for(pos=0; pos<8; pos++) {
25+
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_POS, pos);
26+
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_MASK, 1<<pos);
27+
cb(pos, !!(input & (1<<pos)));
28+
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_MASK, 1<<pos);
29+
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_POS, pos);
30+
}
31+
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
32+
}
33+
34+
static void BitExtractor_down(uint8_t input, callback_t cb)
35+
{
36+
int32_t pos;
37+
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
38+
for(pos=0; pos<8; pos++) {
39+
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_POS, pos);
40+
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_MASK, 0x80>>pos);
41+
cb(pos, !!(input & (0x80>>pos)));
42+
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_MASK, 0x80>>pos);
43+
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_POS, pos);
44+
}
45+
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
46+
}
47+
void BitExtractor(bit_direction_t dir, uint8_t input, callback_t cb)
48+
{
49+
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
50+
if(dir == BIT_DIRECTION_UP) {
51+
BitExtractor_up(input, cb);
52+
} else {
53+
BitExtractor_down(input, cb);
54+
}
55+
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* =========================================================================
2+
Unity - A Test Framework for C
3+
ThrowTheSwitch.org
4+
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
5+
SPDX-License-Identifier: MIT
6+
========================================================================= */
7+
8+
#include <stdint.h>
9+
10+
typedef void callback_t(int position, int bit_value);
11+
typedef enum {
12+
BIT_DIRECTION_UP,
13+
BIT_DIRECTION_DOWN,
14+
} bit_direction_t;
15+
16+
void BitExtractor(bit_direction_t dir, uint8_t input, callback_t cb);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[wrap-git]
2+
url = https://github.com/ThrowTheSwitch/Unity.git
3+
revision = head
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* =========================================================================
2+
Unity - A Test Framework for C
3+
ThrowTheSwitch.org
4+
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
5+
SPDX-License-Identifier: MIT
6+
========================================================================= */
7+
8+
#include "ProductionCode.h"
9+
#include "unity.h"
10+
11+
const int* current_expected_bits = NULL;
12+
UNITY_LINE_TYPE current_vector_line = 0;
13+
typedef struct {
14+
UNITY_LINE_TYPE line;
15+
uint8_t value;
16+
bit_direction_t dir;
17+
int expected_bits[8];
18+
} test_vector_t;
19+
20+
void setUp(void)
21+
{
22+
}
23+
24+
void tearDown(void)
25+
{
26+
}
27+
28+
static void be_bit_tester(int position, int value) {
29+
UNITY_TEST_ASSERT_EQUAL_INT(current_expected_bits[position], value, current_vector_line, "Unexpected bit value");
30+
}
31+
32+
void test_BitExtractor(void)
33+
{
34+
const test_vector_t test_vectors[] = {
35+
{__LINE__, 7, BIT_DIRECTION_UP, {1,1,1,0,0,0,0,0}},
36+
{__LINE__, 7, BIT_DIRECTION_DOWN, {0,0,0,0,0,1,0,1}},
37+
{0}
38+
};
39+
const test_vector_t* tv;
40+
for (tv = test_vectors; tv->line; tv++) {
41+
current_vector_line = tv->line;
42+
current_expected_bits = tv->expected_bits;
43+
BitExtractor(tv->dir, tv->value, be_bit_tester);
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* AUTOGENERATED FILE. DO NOT EDIT. */
2+
3+
/*=======Test Runner Used To Run Each Test Below=====*/
4+
#define RUN_TEST(TestFunc, TestLineNum) \
5+
{ \
6+
Unity.CurrentTestName = #TestFunc; \
7+
Unity.CurrentTestLineNumber = TestLineNum; \
8+
Unity.NumberOfTests++; \
9+
if (TEST_PROTECT()) \
10+
{ \
11+
setUp(); \
12+
TestFunc(); \
13+
} \
14+
if (TEST_PROTECT()) \
15+
{ \
16+
tearDown(); \
17+
} \
18+
UnityConcludeTest(); \
19+
}
20+
21+
/*=======Automagically Detected Files To Include=====*/
22+
#include "unity.h"
23+
#include <setjmp.h>
24+
#include <stdio.h>
25+
#include "ProductionCode.h"
26+
27+
/*=======External Functions This Runner Calls=====*/
28+
extern void setUp(void);
29+
extern void tearDown(void);
30+
extern void test_BitExtractor(void);
31+
32+
33+
/*=======Test Reset Option=====*/
34+
void resetTest(void);
35+
void resetTest(void)
36+
{
37+
tearDown();
38+
setUp();
39+
}
40+
41+
42+
/*=======MAIN=====*/
43+
int main(void)
44+
{
45+
UnityBegin("test/TestProductionCode.c");
46+
RUN_TEST(test_BitExtractor, 32);
47+
return UNITY_END();
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define UNITY_DETAIL_STACK_SIZE 5
2+
#define LABEL_AS_INT32 "#\x18" /*UNITY_DISPLAY_STYLE_INT32 = 0x18 */
3+
#define LABEL_AS_HEX8 "#\x41" /* UNITY_DISPLAY_STYLE_HEX8 = 0x41 */
4+
#define UNITY_DETAIL_LABEL_NAMES { 0, \
5+
UNITY_DETAIL1_NAME, \
6+
UNITY_DETAIL2_NAME, \
7+
"During call", \
8+
LABEL_AS_INT32 "Bit Position", \
9+
LABEL_AS_HEX8 "Bit Mask", \
10+
}
11+
typedef enum {
12+
UNITY_DETAIL_NONE = 0,
13+
UNITY_DETAIL_D1 = 1,
14+
UNITY_DETAIL_D2 = 2,
15+
UNITY_DETAIL_CALL,
16+
UNITY_DETAIL_BIT_POS,
17+
UNITY_DETAIL_BIT_MASK,
18+
} UNITY_DETAIL_LABEL_T;

0 commit comments

Comments
 (0)