mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-23 08:25:58 +01:00
Add example
This commit is contained in:
63
examples/example_5/makefile
Normal file
63
examples/example_5/makefile
Normal file
@@ -0,0 +1,63 @@
|
||||
# =========================================================================
|
||||
# Unity - A Test Framework for C
|
||||
# ThrowTheSwitch.org
|
||||
# Copyright (c) 2007-24 Mike Karlesky, Mark VanderVoord, & Greg Williams
|
||||
# SPDX-License-Identifier: MIT
|
||||
# =========================================================================
|
||||
|
||||
#We try to detect the OS we are running on, and adjust commands as needed
|
||||
ifeq ($(OS),Windows_NT)
|
||||
ifeq ($(shell uname -s),) # not in a bash-like shell
|
||||
CLEANUP = del /F /Q
|
||||
MKDIR = mkdir
|
||||
else # in a bash-like shell, like msys
|
||||
CLEANUP = rm -f
|
||||
MKDIR = mkdir -p
|
||||
endif
|
||||
TARGET_EXTENSION=.exe
|
||||
else
|
||||
CLEANUP = rm -f
|
||||
MKDIR = mkdir -p
|
||||
TARGET_EXTENSION=.out
|
||||
endif
|
||||
|
||||
C_COMPILER=gcc
|
||||
ifeq ($(shell uname -s), Darwin)
|
||||
C_COMPILER=clang
|
||||
endif
|
||||
|
||||
UNITY_ROOT=../..
|
||||
|
||||
CFLAGS=-std=c89
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -Wpointer-arith
|
||||
CFLAGS += -Wcast-align
|
||||
CFLAGS += -Wwrite-strings
|
||||
CFLAGS += -Wswitch-default
|
||||
CFLAGS += -Wunreachable-code
|
||||
CFLAGS += -Winit-self
|
||||
CFLAGS += -Wmissing-field-initializers
|
||||
CFLAGS += -Wno-unknown-pragmas
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
CFLAGS += -Wundef
|
||||
CFLAGS += -Wold-style-definition
|
||||
#CFLAGS += -Wno-misleading-indentation
|
||||
|
||||
|
||||
TARGET_BASE1=test1
|
||||
TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION)
|
||||
SRC_FILES1=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c
|
||||
INC_DIRS=-Isrc -I$(UNITY_ROOT)/src
|
||||
SYMBOLS=-include"test/unity_detail_config.h" -DUNIT_TESTING
|
||||
|
||||
all: clean default
|
||||
|
||||
default: $(SRC_FILES1)
|
||||
$(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1)
|
||||
- ./$(TARGET1)
|
||||
clean:
|
||||
$(CLEANUP) $(TARGET1) $(TARGET2)
|
||||
|
||||
ci: CFLAGS += -Werror
|
||||
ci: default
|
||||
38
examples/example_5/readme.txt
Normal file
38
examples/example_5/readme.txt
Normal file
@@ -0,0 +1,38 @@
|
||||
Example 5
|
||||
=========
|
||||
|
||||
Demonstrate Details Stack usage to implement something similar to a stacktrace.
|
||||
This allows locating the error source much faster in branching/iterating code.
|
||||
|
||||
Build and run with Make
|
||||
---
|
||||
Just run `make`.
|
||||
|
||||
Output
|
||||
---
|
||||
Below the output is annotated with source of the elements.
|
||||
|
||||
```
|
||||
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
|
||||
```
|
||||
|
||||
| String | Source |
|
||||
|-----------------------------|---------------------------------------------------------------------------|
|
||||
| `test/TestProductionCode.c` | `Unity.TestFile` |
|
||||
| `36` | `UNITY_TEST_ASSERT_EQUAL_INT` line |
|
||||
| `test_BitExtractor` | `RUN_TEST` name |
|
||||
| `FAIL` | `UnityStrFail` |
|
||||
| `Expected 1 Was 0` | `UnityAssertEqualNumber` |
|
||||
| `During call` | Detail 0, Label |
|
||||
| `BitExtractor` | Detail 0, Value |
|
||||
| `During call` | Detail 0, Label |
|
||||
| `BitExtractor` | Detail 0, Value |
|
||||
| `During call` | Detail 1, Label |
|
||||
| `BitExtractor_down` | Detail 1, Value |
|
||||
| `Bit Position` | Detail 2, Label (literal starts with #\x18, so value is printed as INT32) |
|
||||
| `6` | Detail 2 Value |
|
||||
| `Bit Mask` | Detail 2, Label (literal starts with #\x41, so value is printed as HEX8) |
|
||||
| `0x02` | Detail 2 Value |
|
||||
| `Unexpected bit value` | `UNITY_TEST_ASSERT_EQUAL_INT` message |
|
||||
|
||||
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`
|
||||
56
examples/example_5/src/ProductionCode.c
Normal file
56
examples/example_5/src/ProductionCode.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* =========================================================================
|
||||
Unity - A Test Framework for C
|
||||
ThrowTheSwitch.org
|
||||
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
|
||||
SPDX-License-Identifier: MIT
|
||||
========================================================================= */
|
||||
|
||||
#include "ProductionCode.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef UNIT_TESTING
|
||||
#include "unity.h"
|
||||
#else
|
||||
/* No-Op when not testing */
|
||||
#define UNITY_DETAIL_PUSH
|
||||
#define UNITY_DETAIL_POP
|
||||
#endif
|
||||
|
||||
static void BitExtractor_up(uint8_t input, callback_t cb)
|
||||
{
|
||||
int32_t pos;
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
for(pos=0; pos<8; pos++) {
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_POS, pos);
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_MASK, 1<<pos);
|
||||
cb(pos, !!(input & (1<<pos)));
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_MASK, 1<<pos);
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_POS, pos);
|
||||
}
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
}
|
||||
|
||||
static void BitExtractor_down(uint8_t input, callback_t cb)
|
||||
{
|
||||
int32_t pos;
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
for(pos=0; pos<8; pos++) {
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_POS, pos);
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_BIT_MASK, 0x80>>pos);
|
||||
cb(pos, !!(input & (0x80>>pos)));
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_MASK, 0x80>>pos);
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_BIT_POS, pos);
|
||||
}
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
}
|
||||
void BitExtractor(bit_direction_t dir, uint8_t input, callback_t cb)
|
||||
{
|
||||
UNITY_DETAIL_PUSH(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
if(dir == BIT_DIRECTION_UP) {
|
||||
BitExtractor_up(input, cb);
|
||||
} else {
|
||||
BitExtractor_down(input, cb);
|
||||
}
|
||||
UNITY_DETAIL_POP(UNITY_DETAIL_CALL, __FUNCTION__);
|
||||
}
|
||||
16
examples/example_5/src/ProductionCode.h
Normal file
16
examples/example_5/src/ProductionCode.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/* =========================================================================
|
||||
Unity - A Test Framework for C
|
||||
ThrowTheSwitch.org
|
||||
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
|
||||
SPDX-License-Identifier: MIT
|
||||
========================================================================= */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void callback_t(int position, int bit_value);
|
||||
typedef enum {
|
||||
BIT_DIRECTION_UP,
|
||||
BIT_DIRECTION_DOWN,
|
||||
} bit_direction_t;
|
||||
|
||||
void BitExtractor(bit_direction_t dir, uint8_t input, callback_t cb);
|
||||
3
examples/example_5/subprojects/unity.wrap
Normal file
3
examples/example_5/subprojects/unity.wrap
Normal file
@@ -0,0 +1,3 @@
|
||||
[wrap-git]
|
||||
url = https://github.com/ThrowTheSwitch/Unity.git
|
||||
revision = head
|
||||
45
examples/example_5/test/TestProductionCode.c
Normal file
45
examples/example_5/test/TestProductionCode.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* =========================================================================
|
||||
Unity - A Test Framework for C
|
||||
ThrowTheSwitch.org
|
||||
Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
|
||||
SPDX-License-Identifier: MIT
|
||||
========================================================================= */
|
||||
|
||||
#include "ProductionCode.h"
|
||||
#include "unity.h"
|
||||
|
||||
const int* current_expected_bits = NULL;
|
||||
UNITY_LINE_TYPE current_vector_line = 0;
|
||||
typedef struct {
|
||||
UNITY_LINE_TYPE line;
|
||||
uint8_t value;
|
||||
bit_direction_t dir;
|
||||
int expected_bits[8];
|
||||
} test_vector_t;
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
}
|
||||
|
||||
static void be_bit_tester(int position, int value) {
|
||||
UNITY_TEST_ASSERT_EQUAL_INT(current_expected_bits[position], value, current_vector_line, "Unexpected bit value");
|
||||
}
|
||||
|
||||
void test_BitExtractor(void)
|
||||
{
|
||||
const test_vector_t test_vectors[] = {
|
||||
{__LINE__, 7, BIT_DIRECTION_UP, {1,1,1,0,0,0,0,0}},
|
||||
{__LINE__, 7, BIT_DIRECTION_DOWN, {0,0,0,0,0,1,0,1}},
|
||||
{0}
|
||||
};
|
||||
const test_vector_t* tv;
|
||||
for (tv = test_vectors; tv->line; tv++) {
|
||||
current_vector_line = tv->line;
|
||||
current_expected_bits = tv->expected_bits;
|
||||
BitExtractor(tv->dir, tv->value, be_bit_tester);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
||||
|
||||
/*=======Test Runner Used To Run Each Test Below=====*/
|
||||
#define RUN_TEST(TestFunc, TestLineNum) \
|
||||
{ \
|
||||
Unity.CurrentTestName = #TestFunc; \
|
||||
Unity.CurrentTestLineNumber = TestLineNum; \
|
||||
Unity.NumberOfTests++; \
|
||||
if (TEST_PROTECT()) \
|
||||
{ \
|
||||
setUp(); \
|
||||
TestFunc(); \
|
||||
} \
|
||||
if (TEST_PROTECT()) \
|
||||
{ \
|
||||
tearDown(); \
|
||||
} \
|
||||
UnityConcludeTest(); \
|
||||
}
|
||||
|
||||
/*=======Automagically Detected Files To Include=====*/
|
||||
#include "unity.h"
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
#include "ProductionCode.h"
|
||||
|
||||
/*=======External Functions This Runner Calls=====*/
|
||||
extern void setUp(void);
|
||||
extern void tearDown(void);
|
||||
extern void test_BitExtractor(void);
|
||||
|
||||
|
||||
/*=======Test Reset Option=====*/
|
||||
void resetTest(void);
|
||||
void resetTest(void)
|
||||
{
|
||||
tearDown();
|
||||
setUp();
|
||||
}
|
||||
|
||||
|
||||
/*=======MAIN=====*/
|
||||
int main(void)
|
||||
{
|
||||
UnityBegin("test/TestProductionCode.c");
|
||||
RUN_TEST(test_BitExtractor, 32);
|
||||
return UNITY_END();
|
||||
}
|
||||
18
examples/example_5/test/unity_detail_config.h
Normal file
18
examples/example_5/test/unity_detail_config.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#define UNITY_DETAIL_STACK_SIZE 5
|
||||
#define LABEL_AS_INT32 "#\x18" /*UNITY_DISPLAY_STYLE_INT32 = 0x18 */
|
||||
#define LABEL_AS_HEX8 "#\x41" /* UNITY_DISPLAY_STYLE_HEX8 = 0x41 */
|
||||
#define UNITY_DETAIL_LABEL_NAMES { 0, \
|
||||
UNITY_DETAIL1_NAME, \
|
||||
UNITY_DETAIL2_NAME, \
|
||||
"During call", \
|
||||
LABEL_AS_INT32 "Bit Position", \
|
||||
LABEL_AS_HEX8 "Bit Mask", \
|
||||
}
|
||||
typedef enum {
|
||||
UNITY_DETAIL_NONE = 0,
|
||||
UNITY_DETAIL_D1 = 1,
|
||||
UNITY_DETAIL_D2 = 2,
|
||||
UNITY_DETAIL_CALL,
|
||||
UNITY_DETAIL_BIT_POS,
|
||||
UNITY_DETAIL_BIT_MASK,
|
||||
} UNITY_DETAIL_LABEL_T;
|
||||
Reference in New Issue
Block a user