This commit is contained in:
Rens Pastoor
2025-05-27 22:41:46 +02:00
parent d141296aea
commit 11b391b8a1
416 changed files with 25232 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
[[_TOC_]]
# Introduction
In this module you will find an introduction the required tooling and hardware you will need for this course.
# Setup
## Linux virtual image
+ Linux Virtual image for programming environment is available at:
+ [www.fhict.nl/docent/downloads/TI/S3/](https://www.fhict.nl/docent/downloads/TI/S3/) (please use the kathara image) This image includes visual studio code setup and kathare (used for networking challenges)
+ If you use your own Linux installation you can use (at your own risk!) the script for the required tooling: [link](https://git.fhict.nl/technology/t-sem2-code/blob/master/es2/scripts/install-tooling.sh).
+ You can download VMware via [FHICT Studentenplein](https://portal.fhict.nl/Studentenplein/SitePages/Home.aspx) via link `VMware Store`.
# Information-resources
## Development environment
For editing and compiling your code we recommend the following options:
+ For Arduino and PC code: [Visual Code](https://code.visualstudio.com/) editor with use of the [PlatformIO](https://platformio.org/install/ide?install=vscode). The provided VmWare image has Visual Code already installed and is configured with a number of plugins to support building executables for your PC and Arduino (see the above installation script for details). You can start up Visual Code from a terminal by the command `code .` (don't forget the `.` (point!): it means it will use the current directory as project directory).
+ For PC-code: a simple text editor (like [Geany](https://www.geany.org/), [Sublime](https://www.sublimetext.com/), [Vim](http://www.vim.org/), [Emacs](https://www.gnu.org/software/emacs/) and a [terminal](https://www.google.nl/search?q=linux+terminal+tutorial) using the [make](https://linux.die.net/man/1/make) program.
+ For building code using a terminal and make see [link](https://www.google.nl/search?q=linux+using+make+terminal).
+ For Arduino code: [Arduino IDE](https://www.arduino.cc/en/Main/Software). We will however require you to use [header-files](https://www.google.nl/search?q=header+files+in+c).
+ An online editor to try some basic stuff: [https://www.onlinegdb.com/](https://www.onlinegdb.com/)
+ Tip: you can install other software in your Linux environment by using [apt-get](https://www.google.nl/search?q=linux+using+apt-get+install).
## For Windows users
It is possible, with some effort on your side, to run C-programs on a Windows machine. This is however at your own initiative to install, and your teacher may offer limited support because of the availability of the Linux image.
### WSL
An option is to use WSL [Windows Subsystem for Linux(WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10). Install the following:
+ [Ubuntu](https://docs.microsoft.com/en-us/windows/wsl/install-win10) and execute the following command in a terminal:
+ `sudo apt-get install -y build-essential make gcc g++`
+ [Visual Studio Code](https://code.visualstudio.com/) met de volgende extensies:
+ [WSL integratie](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl)
+ [PlatformIO](https://marketplace.visualstudio.com/items?itemName=platformio.platformio-ide)
+ For onnecting USB devices to your WSL see [Connect-usb](https://learn.microsoft.com/en-us/windows/wsl/connect-usb#install-the-usbipd-win-project)
### MinGW Gcc compiler
The other option for c-compiling is [TDM-GCC MinGW Compiler](https://sourceforge.net/projects/tdm-gcc/). Here you have to install make and a gcc compiler.
# Starting project
You can use the startingproject in the enclosed folder to test your setup. Content and way of working is explained [here](https://git.fhict.nl/technology/t-oer-prc2/-/blob/master/development-setup/startingProject/README.md).

View File

@@ -0,0 +1,3 @@
!build/
build/*
!build/.gitkeep

View File

@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main_test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

View File

@@ -0,0 +1,41 @@
PROD_DIR := ./product
SHARED_DIR := ./shared
TEST_DIR := ./test
UNITY_FOLDER=./Unity
BUILD_DIR=./build
PROD_EXEC = main
PROD_DIRS := $(PROD_DIR) $(SHARED_DIR)
PROD_FILES := $(wildcard $(patsubst %,%/*.c, $(PROD_DIRS)))
HEADER_PROD_FILES := $(wildcard $(patsubst %,%/*.h, $(PROD_DIRS)))
PROD_INC_DIRS=-I$(PROD_DIR) -I$(SHARED_DIR)
TEST_EXEC = main_test
TEST_DIRS := $(TEST_DIR) $(SHARED_DIR) $(UNITY_FOLDER)
TEST_FILES := $(wildcard $(patsubst %,%/*.c, $(TEST_DIRS)))
HEADER_TEST_FILES := $(wildcard $(patsubst %,%/*.h, $(TEST_DIRS)))
TEST_INC_DIRS=-I$(TEST_DIR) -I$(SHARED_DIR) -I$(UNITY_FOLDER)
CC=gcc
SYMBOLS=-Wall -Werror -g -pedantic -O0 -std=c99
TEST_SYMBOLS=$(SYMBOLS) -DTEST
.PHONY: clean test
all: $(PROD_EXEC) $(TEST_EXEC)
$(PROD_EXEC): Makefile $(PROD_FILES) $(HEADER_FILES)
$(CC) $(PROD_INC_DIRS) $(TEST_SYMBOLS) $(PROD_FILES) -o $(BUILD_DIR)/$(PROD_EXEC)
$(TEST_EXEC): Makefile $(TEST_FILES) $(HEADER_FILES)
$(CC) $(TEST_INC_DIRS) $(TEST_SYMBOLS) $(TEST_FILES) -o $(BUILD_DIR)/$(TEST_EXEC)
run: $(PROD_EXEC)
@./$(BUILD_DIR)/$(PROD_EXEC)
test: $(TEST_EXEC)
@./$(BUILD_DIR)/$(TEST_EXEC)
clean:
rm -f $(BUILD_DIR)/$(PROD_EXEC)
rm -f $(BUILD_DIR)/$(TEST_EXEC)

View File

@@ -0,0 +1,69 @@
# What does this project contain?
This directory holds an (almost) empty C project.
It can be used as a template for your own project.
It contains of:
+ a main which is the starting point for you program
+ an example of a c and corresponding header file
* you have to complete the swap code.
+ an example of a unittest
## Directory structure
Directory | Purpose
---|---
product | Holds the main.c file and c-source files that are **only** needed to build the **main** executable.
test | Holds all the unit tests that are used are build the **main_test** executable.
shared | Holds the files that are used by both the **main** as **main_test** executable. In general these are the c-source files that you wish to unit test.
build | Holds the build [artifacts](https://en.wikipedia.org/wiki/Artifact_(software_development): **main** and **main_test**
# System Preconditions
Preconditions before this project can be used:
+ up and running linux (virtual) machine
+ GIT client up and running
+ working copy of this git repository available
# Project use
To prepare your own working environment it is advised to follow the following steps:
+ open your virtual machine
+ start linux image
+ COPY this project directory to your own GIT archive. Think about the directory where you want put it and how you are going to name it.
+ start visual studio code and load the project
+ start a terminal in visual studio code
# Building the code
You build the code by using a terminal:
Action | Terminal command to type
---|---
Build main code: | make main
Build test code: | make main_test
Build all code: | make all
Run main code: | make run
Run test code: | make test
If you type make run and you get this message:
*failed, the implementation is empty.*
Then you have succesfully installed and ran the code.
If not, you have the fix whatever there needs to be fixed.
# Debugging
This project is configure to debug the test code.
Please refer to [https://code.visualstudio.com/docs/editor/debugging] on how to debug this code.
# Personal projects
If you have succesfully altered this project, commit & push it to your git archive.
Once this is done you have completed one complete development cycle and you can apply this knowledge in challenges to come.

View File

@@ -0,0 +1,278 @@
/* **********************************************
* generic linked list in plain c
* author: Freddy Hurkmans
* date: dec 20, 2014
* **********************************************/
/* ******************************************************************************
* This list is designed using object oriented ideas, but is implemented in c.
*
* The idea is: you construct a list for a certain data structure (say: mystruct):
* list_admin* mylist = construct_list(sizeof(mystruct))
* mylist contains private data for this list implementation. You need not worry
* about it, only give it as first parameter to each list method.
*
* This means you can have multiple lists at the same time, just make sure you
* give the right list_admin structure to the list function.
* When you're done with your list, just call the destructor: destruct_list(&mylist)
* The destructor automatically deletes remaining list elements and the list administration.
*
* As this list implementation keeps its own administration in list_admin, you need
* not worry about next pointers and such. Your structure doesn't even need pointers
* to itself, you only need to worry about data. A valid struct could thus be:
* typedef struct
* {
* int nr;
* char text[100];
* } mystruct;
*
* Adding data to the list is done using list_add_* methods, such as list_add_tail,
* which adds data to the end of the list:
* mystruct data = {10, "hello world!"};
* int result = list_add_tail(mylist, &data);
* You don't have to provide the size of your struct, you've already done that in
* the destructor. If result < 0 list_add_* has failed (either a parameter problem
* or malloc didn't give memory).
*
* You can get a pointer to your data using list_get_*, e.g. get the 2nd element:
* mystruct* dataptr = list_get_element(admin, 1);
* or get the last element:
* mystruct* dataptr = list_get_last(admin);
* If dataptr is not NULL it points to the correct data, else the operation failed.
*
* Searching for data in your list can be done with list_index_of*. list_index_of
* compares the data in each list item to the given data. If they are the same, it
* returns the index. If you want to search for an element with a certain value for
* nr or text (see mystruct) you can implement your own compare function and use
* list_index_of_special (check memcmp for required return values):
* int mycompare(void* p1, void* p2, size_t size)
* {
* // this example doesn't need size!
* mystruct* org = (mystruct*)p1;
* int* nr = (int*)p2;
* return org->nr - nr; // return 0 if they are the same
* }
* Say you want to search for an element that has nr 10:
* int nr = 10;
* int index = list_index_of_special(mylist, &nr, mycompare);
* As noted earlier: mycompare must have the same prototype as memcmp. In fact:
* list_index_of is a shortcut for list_index_of_special(mylist, data, memcmp).
*
* Finally you can delete items with list_delete_*. They should work rather
* straight forward. If they succeed they return 0. You don't have to delete
* all items before destructing your list.
*
* ******************************************************************************/
#include <string.h> /* for memcmp and memcpy */
#include "list.h"
static size_t data_size(const list_admin* admin);
static list_head* get_guaranteed_list_head_of_element_n(list_admin* admin, size_t index);
static list_head* get_list_head_of_element_n(list_admin* admin, size_t index);
static void remove_first_element(list_admin* admin);
static int remove_non_first_element(list_admin* admin, size_t index);
/* **********************************************
* Constructor / destructor
* **********************************************/
list_admin* construct_list(size_t element_size)
{
list_admin* admin = malloc(sizeof(*admin));
if (admin != NULL)
{
admin->head = NULL;
admin->element_size = element_size;
admin->nr_elements = 0;
}
return admin;
}
void destruct_list(list_admin** admin)
{
if ((admin != NULL) && (*admin != NULL))
{
while ((*admin)->head != NULL)
{
list_head* temp = (*admin)->head;
(*admin)->head = (*admin)->head->next;
free(temp);
temp = NULL;
}
free(*admin);
*admin = NULL;
}
}
/* **********************************************
* Public functions
* **********************************************/
int list_get_nr_elements(list_admin* admin)
{
int nr_elements = -1;
if (admin != NULL)
{
nr_elements = admin->nr_elements;
}
return nr_elements;
}
int list_add_tail(list_admin* admin, const void* data)
{
int result = -1;
if ((admin != NULL) && (data != NULL))
{
list_head* new = malloc(data_size(admin));
if (new != NULL)
{
result = 0;
new->next = NULL;
memcpy(new+1, data, admin->element_size);
if (admin->head == NULL)
{
admin->head = new;
}
else
{
list_head* temp = get_guaranteed_list_head_of_element_n(admin, admin->nr_elements-1);
temp->next = new;
}
admin->nr_elements++;
}
}
return result;
}
void* list_get_element(list_admin* admin, size_t index)
{
list_head* item = get_list_head_of_element_n(admin, index);
if(item != NULL)
{
item++; // user data is just beyond list_head, thus we must increase the pointer
}
return item;
}
void* list_get_last(list_admin* admin)
{
list_head* item = NULL;
if (admin != NULL)
{
item = list_get_element(admin, admin->nr_elements-1);
}
return item;
}
int list_index_of(list_admin* admin, const void* data)
{
return list_index_of_special(admin, data, memcmp);
}
int list_index_of_special(list_admin* admin, const void* data, CompareFuncPtr compare)
{
int index = -1;
if ((admin != NULL) && (data != NULL) && (compare != NULL))
{
list_head* temp = admin->head;
index = 0;
while ((temp != NULL) && (compare(temp+1, data, admin->element_size) != 0))
{
temp = temp->next;
index++;
}
if (temp == NULL)
{
index = -1;
}
}
return index;
}
int list_delete_item(list_admin* admin, size_t index)
{
int result = -1;
if ((admin != NULL) && (admin->head != NULL) && (index < admin->nr_elements))
{
if (index == 0)
{
result = 0;
remove_first_element(admin);
}
else
{
result = remove_non_first_element(admin, index);
}
}
return result;
}
int list_delete_last(list_admin* admin)
{
int result = -1;
if (admin != NULL)
{
result = list_delete_item(admin, admin->nr_elements - 1);
}
return result;
}
/* **********************************************
* Private functions
* **********************************************/
static size_t data_size(const list_admin* admin)
{
return admin->element_size + sizeof(list_head);
}
static list_head* get_guaranteed_list_head_of_element_n(list_admin* admin, size_t index)
{
list_head* item = admin->head;
for (size_t i = 0; (i < index) && (item != NULL); i++)
{
item = item->next;
}
return item;
}
static list_head* get_list_head_of_element_n(list_admin* admin, size_t index)
{
list_head* item = NULL;
if ((admin != NULL) && (index < admin->nr_elements))
{
item = get_guaranteed_list_head_of_element_n(admin, index);
}
return item;
}
static void remove_first_element(list_admin* admin)
{
list_head* temp = admin->head;
admin->head = admin->head->next;
free(temp);
temp = NULL;
admin->nr_elements--;
}
static int remove_non_first_element(list_admin* admin, size_t index)
{
int result = -1;
if (index > 0)
{
list_head* temp = get_list_head_of_element_n(admin, index-1);
if ((temp != NULL) && (temp->next != NULL)) // to remove element n, element n-1 and n must exist
{
result = 0;
list_head* to_delete = temp->next;
temp->next = to_delete->next;
free(to_delete);
to_delete = NULL;
admin->nr_elements--;
}
}
return result;
}

View File

@@ -0,0 +1,52 @@
#pragma once
#include <stdlib.h>
typedef struct list_head
{
struct list_head* next;
} list_head;
typedef struct
{
struct list_head* head;
size_t element_size;
size_t nr_elements;
} list_admin;
typedef int (*CompareFuncPtr)(const void*, const void*, size_t);
// call the constructor before using the list:
// list_admin* mylist = construct_list(sizeof(mystruct));
// if mylist equals NULL, no memory could be allocated. Please don't
// mess with the admin data, this can corrupt your data.
list_admin* construct_list(size_t element_size);
// call the destructor when you're done, pass the list administration
// as referenced parameter (the variable will be set to NULL).
void destruct_list(list_admin** admin);
// Reads the number of elements in your list. Returns -1 in case of
// errors, else the number of elements.
int list_get_nr_elements(list_admin* admin);
// Add data to the end of the list. Returns -1 in case of errors, else 0
int list_add_tail(list_admin* admin, const void* data);
// Returns a pointer to the requested element. Returns NULL in case of
// errors (such as: the element does not exist).
void* list_get_element(list_admin* admin, size_t index);
void* list_get_last(list_admin* admin);
// Returns the index to the first found list element that's equal to data,
// or -1 in case of errors (such as: not found).
int list_index_of(list_admin* admin, const void* data);
// Returns the index to the first found list element for which cmp says it's,
// equal to data, or -1 in case of errors (such as: not found). cmp must behave
// just like memcmp, i.e.: return 0 when the data matches.
int list_index_of_special(list_admin* admin, const void* data, CompareFuncPtr cmp);
// Delete an item in the list. Returns -1 in case of errors, else 0.
int list_delete_item(list_admin* admin, size_t index);
int list_delete_last(list_admin* admin);

View File

@@ -0,0 +1,270 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
//#include <malloc.h>
#include <string.h>
#include "resource_detector.h"
#undef malloc
#undef free
#undef main
#undef fopen
#undef fclose
#include "list.h"
#define MAGIC_CODE 0x78563412
#define ADDR(addr,offset) ((addr) + (offset))
#define SET_MAGIC_CODE(addr,offset) *((int*) ADDR(addr,offset)) = MAGIC_CODE
#define MAGIC_CODE_OK(addr,offset) (*((int*) ADDR(addr,offset)) == MAGIC_CODE)
typedef struct
{
char* addr;
unsigned int size;
const char* file;
unsigned int line;
} mem_data;
typedef struct
{
FILE* addr;
const char* file_to_open;
const char* mode;
const char* file;
unsigned int line;
} file_data;
static list_admin* memlist = NULL;
static list_admin* filelist = NULL;
static int memory_compare(const void* meminfo, const void* address, size_t size)
{
mem_data* info = (mem_data*)meminfo;
char* addr = (char*)address;
return info->addr - addr;
}
static int file_compare(const void* fileinfo, const void* address, size_t size)
{
file_data* info = (file_data*)fileinfo;
FILE* addr = (FILE*)address;
return info->addr - addr;
}
static void
invalidate (mem_data* info)
{
char* user_addr;
char* raw_addr;
unsigned int size;
user_addr = info->addr;
size = info->size;
raw_addr = ADDR (user_addr, -sizeof(int));
if (!MAGIC_CODE_OK(user_addr, -sizeof(int)) || !MAGIC_CODE_OK(user_addr, size))
{
fprintf (stderr, "ERROR: addr %p (%s, line %d): out-of-bound access\n", (void*)user_addr, info->file, info->line);
}
memset (raw_addr, 0xff, size + (2 * sizeof(int)));
free (raw_addr);
}
/*
* replacement of malloc
*/
extern void*
xmalloc (unsigned int size, const char* file, unsigned int line)
{
char* raw_addr = malloc (size + (2 * sizeof(int)));
char* user_addr;
mem_data info = {NULL, 0, NULL, 0};
if (raw_addr == NULL)
{
fprintf (stderr, "ERROR: malloc failed for %d bytes (%s, line %d)\n", size, file, line);
return (NULL);
}
else
{
user_addr = ADDR (raw_addr, sizeof (int));
SET_MAGIC_CODE (raw_addr, 0);
SET_MAGIC_CODE (user_addr, size);
info.addr = user_addr;
info.size = size;
info.file = file;
info.line = line;
list_add_tail(memlist, &info);
}
return ((void*) user_addr);
}
/*
* replacement of free
*/
extern void
xfree (void* addr, const char* filename, int linenr)
{
char* user_addr = (char*) addr;
mem_data* info = NULL;
/* check if allocated memory is in our list and retrieve its info */
int index = list_index_of_special(memlist, addr, memory_compare);
info = list_get_element(memlist, index);
if (info == NULL)
{
fprintf (stderr, "ERROR: trying to free memory that was not malloc-ed (addr %p) in %s : %d\n", (void*)user_addr, filename, linenr);
return;
}
invalidate (info);
list_delete_item(memlist, index);
}
static void
print_empty_lines(int n)
{
int i;
for (i = 0; i < n; i++)
{
fprintf(stderr, "\n");
}
}
static void set_colour_red(void)
{
fprintf(stderr, "\033[10;31m");
}
static void set_colour_gray(void)
{
fprintf(stderr, "\033[22;37m");
}
static void
print_line(void)
{
fprintf (stderr, "---------------------------------------------------------\n");
}
static void
print_not_good(void)
{
set_colour_gray();
print_line();
set_colour_red();
fprintf (stderr, " # # ###\n");
fprintf (stderr, " ## # #### ##### #### #### #### ##### ###\n");
fprintf (stderr, " # # # # # # # # # # # # # # ###\n");
fprintf (stderr, " # # # # # # # # # # # # # # \n");
fprintf (stderr, " # # # # # # # ### # # # # # # \n");
fprintf (stderr, " # ## # # # # # # # # # # # ###\n");
fprintf (stderr, " # # #### # #### #### #### ##### ###\n");
set_colour_gray();
print_line();
}
/*
* writes all info of the unallocated memory
*/
static void
resource_detection (void)
{
time_t now = time (NULL);
int forgotten_frees = list_get_nr_elements(memlist);
int nr_files_open = list_get_nr_elements(filelist);
if ((forgotten_frees > 0) || (nr_files_open > 0))
{
print_empty_lines(15);
}
if (forgotten_frees > 0)
{
mem_data* info = NULL;
print_line();
fprintf (stderr, "Memory Leak Summary, generated: %s", ctime (&now));
print_not_good();
set_colour_red();
while((info = list_get_element(memlist, 0)) != NULL)
{
fprintf (stderr, "forgot to free address: %p (%d bytes) that was allocated in: %s on line: %d\n",
(void*)info->addr, info->size, info->file, info->line);
list_delete_item(memlist, 0);
}
set_colour_gray();
print_line();
print_empty_lines(1);
}
if (nr_files_open > 0)
{
file_data* info = NULL;
print_line();
fprintf (stderr, "File Management Summary, generated: %s", ctime (&now));
print_not_good();
set_colour_red();
while((info = list_get_element(filelist, 0)) != NULL)
{
fprintf (stderr, "forgot to close file: %s (ptr %p, mode \"%s\") that was opened from: %s on line: %d\n",
info->file_to_open, (void*)(info->addr), info->mode, info->file, info->line);
list_delete_item(filelist, 0);
}
set_colour_gray();
print_line();
print_empty_lines(1);
}
if ((forgotten_frees == 0) && (nr_files_open == 0))
{
printf ("\nResource checks: OK\n\n");
}
destruct_list(&memlist);
destruct_list(&filelist);
}
extern FILE*
xfopen (const char* file_to_open, const char* mode, const char* filename, int linenr)
{
file_data info = {0};
info.addr = fopen(file_to_open, mode);
if (info.addr != NULL)
{
info.file_to_open = file_to_open;
info.mode = mode;
info.file = filename;
info.line = linenr;
list_add_tail(filelist, &info);
}
return info.addr;
}
extern int
xfclose(FILE* fptr, const char* filename, int linenr)
{
int index = list_index_of_special(filelist, fptr, file_compare);
if (index < 0)
{
fprintf (stderr, "ERROR: trying to close an unopened file in %s on line number %d.\n", filename, linenr);
return -1;
}
list_delete_item(filelist, index);
return (fclose (fptr));
}
extern int
main (int argc, char* argv[])
{
atexit (resource_detection);
memlist = construct_list(sizeof(mem_data));
filelist = construct_list(sizeof(file_data));
return (xmain (argc, argv));
}

View File

@@ -0,0 +1,18 @@
#ifndef RESOURCE_DETECTOR_H
#define RESOURCE_DETECTOR_H
#include <stdio.h>
#define main xmain
#define malloc(size) xmalloc ((size), (__FILE__), (__LINE__))
#define free(addr) xfree ((addr), __FILE__, __LINE__)
#define fopen(name,mode) xfopen ((name),(mode), __FILE__, __LINE__)
#define fclose(fptr) xfclose ((fptr), __FILE__, __LINE__)
extern int xmain(int argc, char* argv[]);
extern void* xmalloc(unsigned int size, const char* file, unsigned int line);
extern void xfree(void* addr, const char* filename, int linenr);
extern FILE* xfopen(const char* file_to_open, const char* mode, const char* filename, int linenr);
extern int xfclose(FILE* fptr, const char* filename, int linenr);
#endif

View File

@@ -0,0 +1,28 @@
UNITY_FOLDER=../Unity
TEST_INC_DIRS=$(INC_DIRS) -I$(UNITY_FOLDER)
TEST_FILES=$(UNITY_FOLDER)/unity.c \
list_test.c \
list.c
HEADER_FILES=*.h
TEST = list_test
CC=gcc
SYMBOLS=-Wall -Werror -pedantic -O0 -ggdb -std=c99
TEST_SYMBOLS=$(SYMBOLS) -DTEST
.PHONY: clean test
all: $(TEST)
$(TEST): Makefile $(TEST_FILES) $(HEADER_FILES)
$(CC) $(TEST_INC_DIRS) $(TEST_SYMBOLS) $(TEST_FILES) -o $(TEST)
clean:
rm -f list $(TEST)
test: $(TEST)
@valgrind ./$(TEST)

View File

@@ -0,0 +1,300 @@
#include <string.h> /* for memcmp */
#include "list.h"
#include "unity.h"
// I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number
#define MY_RUN_TEST(func) RUN_TEST(func, 0)
static list_admin* mylist = NULL;
typedef struct
{
int i;
int j;
} test_struct;
// compare function that returns 0 if test_struct.i matches, it ignores .j
static int compare_test_func(const void* p1, const void* p2, size_t size)
{
size = size; // to prevent warnings
const test_struct* data1 = (const test_struct*)p1;
const test_struct* data2 = (const test_struct*)p2;
return data1->i - data2->i;
}
// check n items in list against verify_data
// IMPORTANT: this function must check using list_get_element,
// else the test for that function is no longer useful
static void check_n_list_elements_ok(list_admin* admin, int nr_items, const test_struct* verify_data)
{
for(int i = 0; i < nr_items; i++)
{
test_struct* ptr = list_get_element(admin, i);
TEST_ASSERT_NOT_NULL(ptr);
TEST_ASSERT_EQUAL(0, memcmp(verify_data+i, ptr, sizeof(*ptr)));
}
}
void setUp(void)
{
// This is run before EACH test
mylist = construct_list(sizeof(test_struct));
TEST_ASSERT_NOT_NULL(mylist);
}
void tearDown(void)
{
// This is run after EACH test
destruct_list(&mylist);
TEST_ASSERT_NULL(mylist);
}
static void test_ConstructList(void)
{
TEST_ASSERT_NULL(mylist->head);
TEST_ASSERT_EQUAL(sizeof(test_struct), mylist->element_size);
TEST_ASSERT_EQUAL(0, mylist->nr_elements);
}
static void test_NrElements_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_get_nr_elements(NULL));
}
static void test_AddData_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_add_tail(mylist, NULL));
TEST_ASSERT_EQUAL(-1, list_add_tail(NULL, mylist));
}
static void test_AddData(void)
{
test_struct data = {3, 4};
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data));
TEST_ASSERT_NOT_NULL(mylist->head);
TEST_ASSERT_EQUAL(1, list_get_nr_elements(mylist));
list_head* head = mylist->head;
test_struct* element = (test_struct*)(head+1);
TEST_ASSERT_EQUAL(data.i, element->i);
TEST_ASSERT_EQUAL(data.j, element->j);
}
static void test_Add2PiecesOfData(void)
{
test_struct data1 = {8, 9};
test_struct data2 = {-3, -4};
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data1));
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data2));
TEST_ASSERT_NOT_NULL(mylist->head);
TEST_ASSERT_NOT_NULL(mylist->head->next);
TEST_ASSERT_EQUAL(2, list_get_nr_elements(mylist));
list_head* head = mylist->head->next;
test_struct* element = (test_struct*)(head+1);
TEST_ASSERT_EQUAL(data2.i, element->i);
TEST_ASSERT_EQUAL(data2.j, element->j);
}
static void test_GetElement_parameters(void)
{
TEST_ASSERT_NULL(list_get_element(NULL, 0));
}
static void test_GetElement(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}};
const int nr_elements = sizeof(data)/sizeof(data[0]);
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
check_n_list_elements_ok(mylist, nr_elements, data); // checks using list_get_element
TEST_ASSERT_NULL(list_get_element(mylist, nr_elements));
TEST_ASSERT_NULL(list_get_element(mylist, -1));
}
static void test_GetLast_parameters(void)
{
TEST_ASSERT_NULL(list_get_last(NULL));
}
static void test_GetLast(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}};
const int nr_elements = sizeof(data)/sizeof(data[0]);
TEST_ASSERT_NULL(list_get_last(mylist));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
test_struct* ptr = list_get_last(mylist);
TEST_ASSERT_NOT_NULL(ptr);
TEST_ASSERT_EQUAL(0, memcmp(&(data[i]), ptr, sizeof(*ptr)));
}
}
static void test_IndexOf_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_index_of(mylist, NULL));
TEST_ASSERT_EQUAL(-1, list_index_of(NULL, mylist));
}
static void test_IndexOf(void)
{
const test_struct real_data[] = {{8, 9}, {-3, -4}, {21, 56}};
const int nr_real_elements = sizeof(real_data)/sizeof(real_data[0]);
const test_struct false_data[] = {{-3, 9}, {8, 56}, {21, -4}, {0, 0}};
const int nr_false_elements = sizeof(false_data)/sizeof(false_data[0]);
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(real_data[i])));
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(i, list_index_of(mylist, &(real_data[i])));
}
for(int i = 0; i < nr_false_elements; i++)
{
TEST_ASSERT_EQUAL(-1, list_index_of(mylist, &(false_data[i])));
}
}
static void test_IndexOfSpecial_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, NULL, compare_test_func));
TEST_ASSERT_EQUAL(-1, list_index_of_special(NULL, mylist, compare_test_func));
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, mylist, NULL));
}
static void test_IndexOfSpecial(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}}; // data in list
const test_struct real_data[] = {{8, -4}, {-3, 56}, {21, 9}}; // data to search for (i equals)
const int nr_real_elements = sizeof(real_data)/sizeof(real_data[0]);
const test_struct false_data[] = {{-2, 9}, {9, -4}, {22, 56}, {0, 0}}; // data that doesn't exist
const int nr_false_elements = sizeof(false_data)/sizeof(false_data[0]);
// first make sure our compare function works
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, compare_test_func(&(data[i]), &(real_data[i]), 0));
for (int j = 0; j < nr_false_elements; j++)
{
TEST_ASSERT_NOT_EQUAL(0, compare_test_func(&(data[i]), &(false_data[j]), 0))
}
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(i, list_index_of_special(mylist, &(real_data[i]), compare_test_func));
}
for(int i = 0; i < nr_false_elements; i++)
{
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, &(false_data[i]), compare_test_func));
}
}
static void test_DeleteItem_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_delete_item(NULL, 0));
TEST_ASSERT_EQUAL(-1, list_delete_item(mylist, -1));
}
static void test_DeleteItem(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
int nr_elements = sizeof(data)/sizeof(data[0]);
const test_struct data_noFirst[] = {{-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
const test_struct data_noLast[] = {{-3, -4}, {21, 56}, {0, 0}};
const test_struct data_noMiddle[] = {{-3, -4}, {0, 0}};
TEST_ASSERT_EQUAL(-1, list_delete_item(mylist, 0));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, 0));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noFirst);
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, nr_elements-1));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noLast);
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, 1));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noMiddle);
}
static void test_DeleteLast_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_delete_last(NULL));
}
static void test_DeleteLast(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
int nr_elements = sizeof(data)/sizeof(data[0]);
TEST_ASSERT_EQUAL(-1, list_delete_last(mylist));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
for (int i = nr_elements-1; i >= 0; i--)
{
TEST_ASSERT_EQUAL(0, list_delete_last(mylist));
TEST_ASSERT_EQUAL(i, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, i, data);
}
TEST_ASSERT_NULL(mylist->head);
TEST_ASSERT_EQUAL(0, list_get_nr_elements(mylist));
}
int main(void)
{
UnityBegin();
MY_RUN_TEST(test_ConstructList);
MY_RUN_TEST(test_NrElements_parameters);
MY_RUN_TEST(test_AddData_parameters);
MY_RUN_TEST(test_AddData);
MY_RUN_TEST(test_Add2PiecesOfData);
MY_RUN_TEST(test_GetElement_parameters);
MY_RUN_TEST(test_GetElement);
MY_RUN_TEST(test_GetLast_parameters);
MY_RUN_TEST(test_GetLast);
MY_RUN_TEST(test_IndexOf_parameters);
MY_RUN_TEST(test_IndexOf);
MY_RUN_TEST(test_IndexOfSpecial_parameters);
MY_RUN_TEST(test_IndexOfSpecial);
MY_RUN_TEST(test_DeleteItem_parameters);
MY_RUN_TEST(test_DeleteItem);
MY_RUN_TEST(test_DeleteLast_parameters);
MY_RUN_TEST(test_DeleteLast);
// MY_RUN_TEST();
// MY_RUN_TEST();
// MY_RUN_TEST();
// MY_RUN_TEST();
return UnityEnd();
}

View File

@@ -0,0 +1,841 @@
/* ==========================================
Unity Project - A Test Framework for C
Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
[Released under MIT License. Please refer to license.txt for details]
========================================== */
#include "unity.h"
#include <stdio.h>
#include <string.h>
#define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_CHAR('\n'); longjmp(Unity.AbortFrame, 1); }
/// return prematurely if we are already in failure or ignore state
#define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); }
struct _Unity Unity = { 0 };
const char* UnityStrNull = "NULL";
const char* UnityStrSpacer = ". ";
const char* UnityStrExpected = " Expected ";
const char* UnityStrWas = " Was ";
const char* UnityStrTo = " To ";
const char* UnityStrElement = " Element ";
const char* UnityStrMemory = " Memory Mismatch";
const char* UnityStrDelta = " Values Not Within Delta ";
const char* UnityStrPointless= " You Asked Me To Compare Nothing, Which Was Pointless.";
const char* UnityStrNullPointerForExpected= " Expected pointer to be NULL";
const char* UnityStrNullPointerForActual = " Actual pointer was NULL";
//-----------------------------------------------
// Pretty Printers & Test Result Output Handlers
//-----------------------------------------------
void UnityPrint(const char* string)
{
const char* pch = string;
if (pch != NULL)
{
while (*pch)
{
// printable characters plus CR & LF are printed
if ((*pch <= 126) && (*pch >= 32))
{
UNITY_OUTPUT_CHAR(*pch);
}
//write escaped carriage returns
else if (*pch == 13)
{
UNITY_OUTPUT_CHAR('\\');
UNITY_OUTPUT_CHAR('r');
}
//write escaped line feeds
else if (*pch == 10)
{
UNITY_OUTPUT_CHAR('\\');
UNITY_OUTPUT_CHAR('n');
}
// unprintable characters are shown as codes
else
{
UNITY_OUTPUT_CHAR('\\');
UnityPrintNumberHex((_U_SINT)*pch, 2);
}
pch++;
}
}
}
//-----------------------------------------------
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style)
{
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{
UnityPrintNumber(number);
}
else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
{
UnityPrintNumberUnsigned((_U_UINT)number);
}
else
{
UnityPrintNumberHex((_U_UINT)number, (style & 0x000F) << 1);
}
}
//-----------------------------------------------
/// basically do an itoa using as little ram as possible
void UnityPrintNumber(const _U_SINT number_to_print)
{
_U_SINT divisor = 1;
_U_SINT next_divisor;
_U_SINT number = number_to_print;
if (number < 0)
{
UNITY_OUTPUT_CHAR('-');
number = -number;
}
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
//-----------------------------------------------
/// basically do an itoa using as little ram as possible
void UnityPrintNumberUnsigned(const _U_UINT number)
{
_U_UINT divisor = 1;
_U_UINT next_divisor;
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
//-----------------------------------------------
void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print)
{
_U_UINT nibble;
char nibbles = nibbles_to_print;
UNITY_OUTPUT_CHAR('0');
UNITY_OUTPUT_CHAR('x');
while (nibbles > 0)
{
nibble = (number >> (--nibbles << 2)) & 0x0000000F;
if (nibble <= 9)
{
UNITY_OUTPUT_CHAR((char)('0' + nibble));
}
else
{
UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));
}
}
}
//-----------------------------------------------
void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
{
_U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1);
_US32 i;
for (i = 0; i < UNITY_INT_WIDTH; i++)
{
if (current_bit & mask)
{
if (current_bit & number)
{
UNITY_OUTPUT_CHAR('1');
}
else
{
UNITY_OUTPUT_CHAR('0');
}
}
else
{
UNITY_OUTPUT_CHAR('X');
}
current_bit = current_bit >> 1;
}
}
//-----------------------------------------------
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(_UF number)
{
char TempBuffer[32];
sprintf(TempBuffer, "%.6f", number);
UnityPrint(TempBuffer);
}
#endif
//-----------------------------------------------
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
{
UnityPrint(file);
UNITY_OUTPUT_CHAR(':');
UnityPrintNumber(line);
UNITY_OUTPUT_CHAR(':');
UnityPrint(Unity.CurrentTestName);
UNITY_OUTPUT_CHAR(':');
}
//-----------------------------------------------
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
{
UnityTestResultsBegin(Unity.TestFile, line);
UnityPrint("FAIL:");
}
//-----------------------------------------------
void UnityConcludeTest(void)
{
if (Unity.CurrentTestIgnored)
{
Unity.TestIgnores++;
}
else if (!Unity.CurrentTestFailed)
{
UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);
UnityPrint("PASS");
UNITY_PRINT_EOL;
}
else
{
Unity.TestFailures++;
}
Unity.CurrentTestFailed = 0;
Unity.CurrentTestIgnored = 0;
}
//-----------------------------------------------
void UnityAddMsgIfSpecified(const char* msg)
{
if (msg)
{
UnityPrint(UnityStrSpacer);
UnityPrint(msg);
}
}
//-----------------------------------------------
void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
{
UNITY_OUTPUT_CHAR('\'');
UnityPrint(expected);
UNITY_OUTPUT_CHAR('\'');
}
else
{
UnityPrint(UnityStrNull);
}
UnityPrint(UnityStrWas);
if (actual != NULL)
{
UNITY_OUTPUT_CHAR('\'');
UnityPrint(actual);
UNITY_OUTPUT_CHAR('\'');
}
else
{
UnityPrint(UnityStrNull);
}
}
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------
int UnityCheckArraysForNull(const void* expected, const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
{
//return true if they are both NULL
if ((expected == NULL) && (actual == NULL))
return 1;
//throw error if just expected is NULL
if (expected == NULL)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrNullPointerForExpected);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
//throw error if just actual is NULL
if (actual == NULL)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrNullPointerForActual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
//return false if neither is NULL
return 0;
}
//-----------------------------------------------
// Assertion Functions
//-----------------------------------------------
void UnityAssertBits(const _U_SINT mask,
const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_SKIP_EXECUTION;
if ((mask & expected) != (mask & actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintMask(mask, expected);
UnityPrint(UnityStrWas);
UnityPrintMask(mask, actual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
//-----------------------------------------------
void UnityAssertEqualNumber(const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
UNITY_SKIP_EXECUTION;
if (expected != actual)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
//-----------------------------------------------
void UnityAssertEqualIntArray(const _U_SINT* expected,
const _U_SINT* actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
_UU32 elements = num_elements;
const _US8* ptr_exp = (_US8*)expected;
const _US8* ptr_act = (_US8*)actual;
UNITY_SKIP_EXECUTION;
if (elements == 0)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
return;
switch(style)
{
case UNITY_DISPLAY_STYLE_HEX8:
case UNITY_DISPLAY_STYLE_INT8:
case UNITY_DISPLAY_STYLE_UINT8:
while (elements--)
{
if (*ptr_exp != *ptr_act)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*ptr_exp, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*ptr_act, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_exp += 1;
ptr_act += 1;
}
break;
case UNITY_DISPLAY_STYLE_HEX16:
case UNITY_DISPLAY_STYLE_INT16:
case UNITY_DISPLAY_STYLE_UINT16:
while (elements--)
{
if (*(_US16*)ptr_exp != *(_US16*)ptr_act)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*(_US16*)ptr_exp, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*(_US16*)ptr_act, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_exp += 2;
ptr_act += 2;
}
break;
#ifdef UNITY_SUPPORT_64
case UNITY_DISPLAY_STYLE_HEX64:
case UNITY_DISPLAY_STYLE_INT64:
case UNITY_DISPLAY_STYLE_UINT64:
while (elements--)
{
if (*(_US64*)ptr_exp != *(_US64*)ptr_act)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*(_US64*)ptr_exp, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*(_US64*)ptr_act, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_exp += 8;
ptr_act += 8;
}
break;
#endif
default:
while (elements--)
{
if (*(_US32*)ptr_exp != *(_US32*)ptr_act)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*(_US32*)ptr_exp, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*(_US32*)ptr_act, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_exp += 4;
ptr_act += 4;
}
break;
}
}
//-----------------------------------------------
#ifndef UNITY_EXCLUDE_FLOAT
void UnityAssertEqualFloatArray(const _UF* expected,
const _UF* actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
_UU32 elements = num_elements;
const _UF* ptr_expected = expected;
const _UF* ptr_actual = actual;
_UF diff, tol;
UNITY_SKIP_EXECUTION;
if (elements == 0)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
return;
while (elements--)
{
diff = *ptr_expected - *ptr_actual;
if (diff < 0.0)
diff = 0.0 - diff;
tol = UNITY_FLOAT_PRECISION * *ptr_expected;
if (tol < 0.0)
tol = 0.0 - tol;
if (diff > tol)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
#ifdef UNITY_FLOAT_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat(*ptr_expected);
UnityPrint(UnityStrWas);
UnityPrintFloat(*ptr_actual);
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_expected++;
ptr_actual++;
}
}
//-----------------------------------------------
void UnityAssertFloatsWithin(const _UF delta,
const _UF expected,
const _UF actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
_UF diff = actual - expected;
_UF pos_delta = delta;
UNITY_SKIP_EXECUTION;
if (diff < 0)
{
diff = 0.0f - diff;
}
if (pos_delta < 0)
{
pos_delta = 0.0f - pos_delta;
}
if (pos_delta < diff)
{
UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_FLOAT_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat(expected);
UnityPrint(UnityStrWas);
UnityPrintFloat(actual);
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
#endif
//-----------------------------------------------
void UnityAssertNumbersWithin( const _U_SINT delta,
const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
UNITY_SKIP_EXECUTION;
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{
if (actual > expected)
Unity.CurrentTestFailed = ((actual - expected) > delta);
else
Unity.CurrentTestFailed = ((expected - actual) > delta);
}
else
{
if ((_U_UINT)actual > (_U_UINT)expected)
Unity.CurrentTestFailed = ((_U_UINT)(actual - expected) > (_U_UINT)delta);
else
Unity.CurrentTestFailed = ((_U_UINT)(expected - actual) > (_U_UINT)delta);
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrDelta);
UnityPrintNumberByStyle(delta, style);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
//-----------------------------------------------
void UnityAssertEqualString(const char* expected,
const char* actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
_UU32 i;
UNITY_SKIP_EXECUTION;
// if both pointers not null compare the strings
if (expected && actual)
{
for (i = 0; expected[i] || actual[i]; i++)
{
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
break;
}
}
}
else
{ // handle case of one pointers being null (if both null, test should pass)
if (expected != actual)
{
Unity.CurrentTestFailed = 1;
}
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStrings(expected, actual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
//-----------------------------------------------
void UnityAssertEqualStringArray( const char** expected,
const char** actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
_UU32 i, j = 0;
UNITY_SKIP_EXECUTION;
// if no elements, it's an error
if (num_elements == 0)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
return;
do
{
// if both pointers not null compare the strings
if (expected[j] && actual[j])
{
for (i = 0; expected[j][i] || actual[j][i]; i++)
{
if (expected[j][i] != actual[j][i])
{
Unity.CurrentTestFailed = 1;
break;
}
}
}
else
{ // handle case of one pointers being null (if both null, test should pass)
if (expected[j] != actual[j])
{
Unity.CurrentTestFailed = 1;
}
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
if (num_elements > 1)
{
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - j - 1), UNITY_DISPLAY_STYLE_UINT);
}
UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
} while (++j < num_elements);
}
//-----------------------------------------------
void UnityAssertEqualMemory( const void* expected,
const void* actual,
_UU32 length,
_UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
unsigned char* expected_ptr = (unsigned char*)expected;
unsigned char* actual_ptr = (unsigned char*)actual;
_UU32 elements = num_elements;
UNITY_SKIP_EXECUTION;
if ((elements == 0) || (length == 0))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((void*)expected, (void*)actual, lineNumber, msg) == 1)
return;
while (elements--)
{
if (memcmp((const void*)expected_ptr, (const void*)actual_ptr, length) != 0)
{
Unity.CurrentTestFailed = 1;
break;
}
expected_ptr += length;
actual_ptr += length;
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
if (num_elements > 1)
{
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
}
UnityPrint(UnityStrMemory);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
//-----------------------------------------------
// Control Functions
//-----------------------------------------------
void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
{
UNITY_SKIP_EXECUTION;
UnityTestResultsBegin(Unity.TestFile, line);
UnityPrint("FAIL");
if (msg != NULL)
{
UNITY_OUTPUT_CHAR(':');
if (msg[0] != ' ')
{
UNITY_OUTPUT_CHAR(' ');
}
UnityPrint(msg);
}
UNITY_FAIL_AND_BAIL;
}
//-----------------------------------------------
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
{
UNITY_SKIP_EXECUTION;
UnityTestResultsBegin(Unity.TestFile, line);
UnityPrint("IGNORE");
if (msg != NULL)
{
UNITY_OUTPUT_CHAR(':');
UNITY_OUTPUT_CHAR(' ');
UnityPrint(msg);
}
UNITY_IGNORE_AND_BAIL;
}
//-----------------------------------------------
void setUp(void);
void tearDown(void);
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
{
Unity.CurrentTestName = FuncName;
Unity.CurrentTestLineNumber = FuncLineNum;
Unity.NumberOfTests++;
if (TEST_PROTECT())
{
setUp();
Func();
}
if (TEST_PROTECT() && !(Unity.CurrentTestIgnored))
{
tearDown();
}
UnityConcludeTest();
}
//-----------------------------------------------
void UnityBegin(void)
{
Unity.NumberOfTests = 0;
}
//-----------------------------------------------
int UnityEnd(void)
{
UnityPrint("-----------------------");
UNITY_PRINT_EOL;
UnityPrintNumber(Unity.NumberOfTests);
UnityPrint(" Tests ");
UnityPrintNumber(Unity.TestFailures);
UnityPrint(" Failures ");
UnityPrintNumber(Unity.TestIgnores);
UnityPrint(" Ignored");
UNITY_PRINT_EOL;
if (Unity.TestFailures == 0U)
{
UnityPrint("OK");
}
else
{
UnityPrint("FAIL");
}
UNITY_PRINT_EOL;
return Unity.TestFailures;
}

View File

@@ -0,0 +1,209 @@
/* ==========================================
Unity Project - A Test Framework for C
Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
[Released under MIT License. Please refer to license.txt for details]
========================================== */
#ifndef UNITY_FRAMEWORK_H
#define UNITY_FRAMEWORK_H
#define UNITY
#include "unity_internals.h"
//-------------------------------------------------------
// Configuration Options
//-------------------------------------------------------
// Integers
// - Unity assumes 32 bit integers by default
// - If your compiler treats ints of a different size, define UNITY_INT_WIDTH
// Floats
// - define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons
// - define UNITY_FLOAT_PRECISION to specify the precision to use when doing TEST_ASSERT_EQUAL_FLOAT
// - define UNITY_FLOAT_TYPE to specify doubles instead of single precision floats
// - define UNITY_FLOAT_VERBOSE to print floating point values in errors (uses sprintf)
// Output
// - by default, Unity prints to standard out with putchar. define UNITY_OUTPUT_CHAR(a) with a different function if desired
// Optimization
// - by default, line numbers are stored in unsigned shorts. Define UNITY_LINE_TYPE with a different type if your files are huge
// - by default, test and failure counters are unsigned shorts. Define UNITY_COUNTER_TYPE with a different type if you want to save space or have more than 65535 Tests.
//-------------------------------------------------------
// Test Running Macros
//-------------------------------------------------------
#define TEST_PROTECT() (setjmp(Unity.AbortFrame) == 0)
#define TEST_ABORT() {longjmp(Unity.AbortFrame, 1);}
#ifndef RUN_TEST
#define RUN_TEST(func, line_num) UnityDefaultTestRun(func, #func, line_num)
#endif
#define TEST_LINE_NUM (Unity.CurrentTestLineNumber)
#define TEST_IS_IGNORED (Unity.CurrentTestIgnored)
#define TEST_CASE(...)
//-------------------------------------------------------
// Basic Fail and Ignore
//-------------------------------------------------------
#define TEST_FAIL_MESSAGE(message) UNITY_TEST_FAIL(__LINE__, message)
#define TEST_FAIL() UNITY_TEST_FAIL(__LINE__, NULL)
#define TEST_IGNORE_MESSAGE(message) UNITY_TEST_IGNORE(__LINE__, message)
#define TEST_IGNORE() UNITY_TEST_IGNORE(__LINE__, NULL)
#define TEST_ONLY()
//-------------------------------------------------------
// Test Asserts (simple)
//-------------------------------------------------------
//Boolean
#define TEST_ASSERT(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expression Evaluated To FALSE")
#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE")
#define TEST_ASSERT_UNLESS(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expression Evaluated To TRUE")
#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE")
#define TEST_ASSERT_NULL(pointer) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, " Expected NULL")
#define TEST_ASSERT_NOT_NULL(pointer) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, " Expected Non-NULL")
//Integers (of all sizes)
#define TEST_ASSERT_EQUAL_INT(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT8(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT8((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT16(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT16((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT32(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT32((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT64(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT64((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_NOT_EQUAL(expected, actual) UNITY_TEST_ASSERT(((expected) != (actual)), __LINE__, " Expected Not-Equal")
#define TEST_ASSERT_EQUAL_UINT(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT8(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT8( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT16(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT16( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT32(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT32( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT64(expected, actual) UNITY_TEST_ASSERT_EQUAL_UINT64( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX8(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX8( (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX16(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX16((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX32(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX64(expected, actual) UNITY_TEST_ASSERT_EQUAL_HEX64((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_BITS(mask, expected, actual) UNITY_TEST_ASSERT_BITS((mask), (expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_BITS_HIGH(mask, actual) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(-1), (actual), __LINE__, NULL)
#define TEST_ASSERT_BITS_LOW(mask, actual) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(0), (actual), __LINE__, NULL)
#define TEST_ASSERT_BIT_HIGH(bit, actual) UNITY_TEST_ASSERT_BITS(((_UU32)1 << bit), (_UU32)(-1), (actual), __LINE__, NULL)
#define TEST_ASSERT_BIT_LOW(bit, actual) UNITY_TEST_ASSERT_BITS(((_UU32)1 << bit), (_UU32)(0), (actual), __LINE__, NULL)
//Integer Ranges (of all sizes)
#define TEST_ASSERT_INT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_INT_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_UINT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_UINT_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_HEX_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_HEX8_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX8_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_HEX16_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX16_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_HEX32_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_HEX64_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, __LINE__, NULL)
//Structs and Strings
#define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, NULL)
//Arrays
#define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, __LINE__, NULL)
//Floating Point (If Enabled)
#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, __LINE__, NULL)
#define TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, __LINE__, NULL)
//-------------------------------------------------------
// Test Asserts (with additional messages)
//-------------------------------------------------------
//Boolean
#define TEST_ASSERT_MESSAGE(condition, message) UNITY_TEST_ASSERT( (condition), __LINE__, message)
#define TEST_ASSERT_TRUE_MESSAGE(condition, message) UNITY_TEST_ASSERT( (condition), __LINE__, message)
#define TEST_ASSERT_UNLESS_MESSAGE(condition, message) UNITY_TEST_ASSERT( !(condition), __LINE__, message)
#define TEST_ASSERT_FALSE_MESSAGE(condition, message) UNITY_TEST_ASSERT( !(condition), __LINE__, message)
#define TEST_ASSERT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, message)
#define TEST_ASSERT_NOT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, message)
//Integers (of all sizes)
#define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_INT8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT8((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_INT16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT16((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_INT32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT32((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_INT64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT64((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, message)
#define TEST_ASSERT_NOT_EQUAL_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT(((expected) != (actual)), __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT8( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT16( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT32( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_UINT64( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX8_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX8( (expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX16_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX16((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX32_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX32((expected), (actual), __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX64_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_HEX64((expected), (actual), __LINE__, message)
#define TEST_ASSERT_BITS_MESSAGE(mask, expected, actual, message) UNITY_TEST_ASSERT_BITS((mask), (expected), (actual), __LINE__, message)
#define TEST_ASSERT_BITS_HIGH_MESSAGE(mask, actual, message) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(-1), (actual), __LINE__, message)
#define TEST_ASSERT_BITS_LOW_MESSAGE(mask, actual, message) UNITY_TEST_ASSERT_BITS((mask), (_UU32)(0), (actual), __LINE__, message)
#define TEST_ASSERT_BIT_HIGH_MESSAGE(bit, actual, message) UNITY_TEST_ASSERT_BITS(((_UU32)1 << bit), (_UU32)(-1), (actual), __LINE__, message)
#define TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, message) UNITY_TEST_ASSERT_BITS(((_UU32)1 << bit), (_UU32)(0), (actual), __LINE__, message)
//Integer Ranges (of all sizes)
#define TEST_ASSERT_INT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_INT_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_UINT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_UINT_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_HEX_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_HEX8_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX8_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_HEX16_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX16_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_HEX32_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_HEX64_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, __LINE__, message)
//Structs and Strings
#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, __LINE__, message)
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, __LINE__, message)
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, __LINE__, message)
//Arrays
#define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_INT8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_INT16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_INT32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_INT64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_UINT64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_HEX64_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, __LINE__, message)
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, message) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, __LINE__, message)
//Floating Point (If Enabled)
#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, __LINE__, message)
#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, __LINE__, message)
#define TEST_ASSERT_EQUAL_FLOAT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, __LINE__, message)
#endif

View File

@@ -0,0 +1,356 @@
/* ==========================================
Unity Project - A Test Framework for C
Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
[Released under MIT License. Please refer to license.txt for details]
========================================== */
#ifndef UNITY_INTERNALS_H
#define UNITY_INTERNALS_H
#include <stdio.h>
#include <setjmp.h>
//-------------------------------------------------------
// Int Support
//-------------------------------------------------------
#ifndef UNITY_INT_WIDTH
#define UNITY_INT_WIDTH (32)
#endif
#ifndef UNITY_LONG_WIDTH
#define UNITY_LONG_WIDTH (32)
#endif
#if (UNITY_INT_WIDTH == 32)
typedef unsigned char _UU8;
typedef unsigned short _UU16;
typedef unsigned int _UU32;
typedef signed char _US8;
typedef signed short _US16;
typedef signed int _US32;
#elif (UNITY_INT_WIDTH == 16)
typedef unsigned char _UU8;
typedef unsigned int _UU16;
typedef unsigned long _UU32;
typedef signed char _US8;
typedef signed int _US16;
typedef signed long _US32;
#else
#error Invalid UNITY_INT_WIDTH specified! (16 or 32 are supported)
#endif
//-------------------------------------------------------
// 64-bit Support
//-------------------------------------------------------
#ifndef UNITY_SUPPORT_64
//No 64-bit Support
typedef _UU32 _U_UINT;
typedef _US32 _U_SINT;
#else
//64-bit Support
#if (UNITY_LONG_WIDTH == 32)
typedef unsigned long long _UU64;
typedef signed long long _US64;
#elif (UNITY_LONG_WIDTH == 64)
typedef unsigned long _UU64;
typedef signed long _US64;
#else
#error Invalid UNITY_LONG_WIDTH specified! (32 or 64 are supported)
#endif
typedef _UU64 _U_UINT;
typedef _US64 _U_SINT;
#endif
//-------------------------------------------------------
// Pointer Support
//-------------------------------------------------------
#ifndef UNITY_POINTER_WIDTH
#define UNITY_POINTER_WIDTH (32)
#endif
#if (UNITY_POINTER_WIDTH == 32)
typedef _UU32 _UP;
#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX32
#elif (UNITY_POINTER_WIDTH == 64)
typedef _UU64 _UP;
#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX64
#elif (UNITY_POINTER_WIDTH == 16)
typedef _UU16 _UP;
#define UNITY_DISPLAY_STYLE_POINTER UNITY_DISPLAY_STYLE_HEX16
#else
#error Invalid UNITY_POINTER_WIDTH specified! (16, 32 or 64 are supported)
#endif
//-------------------------------------------------------
// Float Support
//-------------------------------------------------------
#ifdef UNITY_EXCLUDE_FLOAT
//No Floating Point Support
#undef UNITY_FLOAT_PRECISION
#undef UNITY_FLOAT_TYPE
#undef UNITY_FLOAT_VERBOSE
#else
//Floating Point Support
#ifndef UNITY_FLOAT_PRECISION
#define UNITY_FLOAT_PRECISION (0.00001f)
#endif
#ifndef UNITY_FLOAT_TYPE
#define UNITY_FLOAT_TYPE float
#endif
typedef UNITY_FLOAT_TYPE _UF;
#endif
//-------------------------------------------------------
// Output Method
//-------------------------------------------------------
#ifndef UNITY_OUTPUT_CHAR
//Default to using putchar, which is defined in stdio.h above
#define UNITY_OUTPUT_CHAR(a) putchar(a)
#else
//If defined as something else, make sure we declare it here so it's ready for use
extern int UNITY_OUTPUT_CHAR(int);
#endif
//-------------------------------------------------------
// Footprint
//-------------------------------------------------------
#ifndef UNITY_LINE_TYPE
#define UNITY_LINE_TYPE unsigned short
#endif
#ifndef UNITY_COUNTER_TYPE
#define UNITY_COUNTER_TYPE unsigned short
#endif
//-------------------------------------------------------
// Internal Structs Needed
//-------------------------------------------------------
typedef void (*UnityTestFunction)(void);
#define UNITY_DISPLAY_RANGE_INT (0x10)
#define UNITY_DISPLAY_RANGE_UINT (0x20)
#define UNITY_DISPLAY_RANGE_HEX (0x40)
#define UNITY_DISPLAY_RANGE_AUTO (0x80)
typedef enum
{
UNITY_DISPLAY_STYLE_INT = 4 + UNITY_DISPLAY_RANGE_INT + UNITY_DISPLAY_RANGE_AUTO,
UNITY_DISPLAY_STYLE_INT8 = 1 + UNITY_DISPLAY_RANGE_INT,
UNITY_DISPLAY_STYLE_INT16 = 2 + UNITY_DISPLAY_RANGE_INT,
UNITY_DISPLAY_STYLE_INT32 = 4 + UNITY_DISPLAY_RANGE_INT,
#ifdef UNITY_SUPPORT_64
UNITY_DISPLAY_STYLE_INT64 = 8 + UNITY_DISPLAY_RANGE_INT,
#endif
UNITY_DISPLAY_STYLE_UINT = 4 + UNITY_DISPLAY_RANGE_UINT + UNITY_DISPLAY_RANGE_AUTO,
UNITY_DISPLAY_STYLE_UINT8 = 1 + UNITY_DISPLAY_RANGE_UINT,
UNITY_DISPLAY_STYLE_UINT16 = 2 + UNITY_DISPLAY_RANGE_UINT,
UNITY_DISPLAY_STYLE_UINT32 = 4 + UNITY_DISPLAY_RANGE_UINT,
#ifdef UNITY_SUPPORT_64
UNITY_DISPLAY_STYLE_UINT64 = 8 + UNITY_DISPLAY_RANGE_UINT,
#endif
UNITY_DISPLAY_STYLE_HEX8 = 1 + UNITY_DISPLAY_RANGE_HEX,
UNITY_DISPLAY_STYLE_HEX16 = 2 + UNITY_DISPLAY_RANGE_HEX,
UNITY_DISPLAY_STYLE_HEX32 = 4 + UNITY_DISPLAY_RANGE_HEX,
#ifdef UNITY_SUPPORT_64
UNITY_DISPLAY_STYLE_HEX64 = 8 + UNITY_DISPLAY_RANGE_HEX,
#endif
} UNITY_DISPLAY_STYLE_T;
struct _Unity
{
const char* TestFile;
const char* CurrentTestName;
_UU32 CurrentTestLineNumber;
UNITY_COUNTER_TYPE NumberOfTests;
UNITY_COUNTER_TYPE TestFailures;
UNITY_COUNTER_TYPE TestIgnores;
UNITY_COUNTER_TYPE CurrentTestFailed;
UNITY_COUNTER_TYPE CurrentTestIgnored;
jmp_buf AbortFrame;
};
extern struct _Unity Unity;
//-------------------------------------------------------
// Test Suite Management
//-------------------------------------------------------
void UnityBegin(void);
int UnityEnd(void);
void UnityConcludeTest(void);
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum);
//-------------------------------------------------------
// Test Output
//-------------------------------------------------------
void UnityPrint(const char* string);
void UnityPrintMask(const _U_UINT mask, const _U_UINT number);
void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style);
void UnityPrintNumber(const _U_SINT number);
void UnityPrintNumberUnsigned(const _U_UINT number);
void UnityPrintNumberHex(const _U_UINT number, const char nibbles);
#ifdef UNITY_FLOAT_VERBOSE
void UnityPrintFloat(const _UF number);
#endif
//-------------------------------------------------------
// Test Assertion Fuctions
//-------------------------------------------------------
// Use the macros below this section instead of calling
// these directly. The macros have a consistent naming
// convention and will pull in file and line information
// for you.
void UnityAssertEqualNumber(const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style);
void UnityAssertEqualIntArray(const _U_SINT* expected,
const _U_SINT* actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style);
void UnityAssertBits(const _U_SINT mask,
const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
void UnityAssertEqualString(const char* expected,
const char* actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
void UnityAssertEqualStringArray( const char** expected,
const char** actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
void UnityAssertEqualMemory( const void* expected,
const void* actual,
const _UU32 length,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
void UnityAssertNumbersWithin(const _U_SINT delta,
const _U_SINT expected,
const _U_SINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style);
void UnityFail(const char* message, const UNITY_LINE_TYPE line);
void UnityIgnore(const char* message, const UNITY_LINE_TYPE line);
#ifndef UNITY_EXCLUDE_FLOAT
void UnityAssertFloatsWithin(const _UF delta,
const _UF expected,
const _UF actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
void UnityAssertEqualFloatArray(const _UF* expected,
const _UF* actual,
const _UU32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber);
#endif
//-------------------------------------------------------
// Basic Fail and Ignore
//-------------------------------------------------------
#define UNITY_TEST_FAIL(line, message) UnityFail( (message), (UNITY_LINE_TYPE)line);
#define UNITY_TEST_IGNORE(line, message) UnityIgnore( (message), (UNITY_LINE_TYPE)line);
//-------------------------------------------------------
// Test Asserts
//-------------------------------------------------------
#define UNITY_TEST_ASSERT(condition, line, message) if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)line, message);}
#define UNITY_TEST_ASSERT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) == NULL), (UNITY_LINE_TYPE)line, message)
#define UNITY_TEST_ASSERT_NOT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) != NULL), (UNITY_LINE_TYPE)line, message)
#define UNITY_TEST_ASSERT_EQUAL_INT(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_EQUAL_INT8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_EQUAL_INT16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_EQUAL_INT32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_EQUAL_UINT(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_EQUAL_UINT8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_EQUAL_UINT16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_EQUAL_UINT32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_EQUAL_HEX8(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX8)
#define UNITY_TEST_ASSERT_EQUAL_HEX16(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX16)
#define UNITY_TEST_ASSERT_EQUAL_HEX32(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX32)
#define UNITY_TEST_ASSERT_BITS(mask, expected, actual, line, message) UnityAssertBits((_U_SINT)(mask), (_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line)
#define UNITY_TEST_ASSERT_INT_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_UINT_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_HEX8_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX8)
#define UNITY_TEST_ASSERT_HEX16_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX16)
#define UNITY_TEST_ASSERT_HEX32_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX32)
#define UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((_U_SINT)(delta), (_U_SINT)(expected), (_U_SINT)(actual), NULL, (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX64)
#define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(_UP)(expected), (_U_SINT)(_UP)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_POINTER)
#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)line)
#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((void*)(expected), (void*)(actual), (_UU32)(len), 1, (message), (UNITY_LINE_TYPE)line)
#define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT)
#define UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT8)
#define UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT16)
#define UNITY_TEST_ASSERT_EQUAL_INT32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT32)
#define UNITY_TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT)
#define UNITY_TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT8)
#define UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT16)
#define UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT32)
#define UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX8)
#define UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX16)
#define UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX32)
#define UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((const char**)(expected), (const char**)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line)
#define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((void*)(expected), (void*)(actual), (_UU32)(len), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line)
#ifdef UNITY_SUPPORT_64
#define UNITY_TEST_ASSERT_EQUAL_INT64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT64)
#define UNITY_TEST_ASSERT_EQUAL_UINT64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT64)
#define UNITY_TEST_ASSERT_EQUAL_HEX64(expected, actual, line, message) UnityAssertEqualNumber((_U_SINT)(expected), (_U_SINT)(actual), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX64)
#define UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_INT64)
#define UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_UINT64)
#define UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _U_SINT*)(expected), (const _U_SINT*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX64)
#endif
#ifdef UNITY_EXCLUDE_FLOAT
#define UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)line, "Unity Floating Point Disabled")
#define UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)line, "Unity Floating Point Disabled")
#define UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)line, "Unity Floating Point Disabled")
#else
#define UNITY_TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual, line, message) UnityAssertFloatsWithin((_UF)(delta), (_UF)(expected), (_UF)(actual), (message), (UNITY_LINE_TYPE)line)
#define UNITY_TEST_ASSERT_EQUAL_FLOAT(expected, actual, line, message) UNITY_TEST_ASSERT_FLOAT_WITHIN((_UF)(expected) * (_UF)UNITY_FLOAT_PRECISION, (_UF)expected, (_UF)actual, (UNITY_LINE_TYPE)line, message)
#define UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualFloatArray((_UF*)(expected), (_UF*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line)
#endif
#endif

View File

@@ -0,0 +1,97 @@
#include "unity_test_module.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "unity.h"
void (*unity_setUp_ptr)(void) = NULL;
void (*unit_tearDown_ptr)(void) = NULL;
#ifdef UNITY_USE_MODULE_SETUP_TEARDOWN
void setUp()
{
if(unity_setUp_ptr != NULL) unity_setUp_ptr();
}
void tearDown()
{
if(unit_tearDown_ptr != NULL) unit_tearDown_ptr();
}
#endif
void UnityRegisterSetupTearDown( void(*setUp)(void), void(*tearDown)(void) )
{
unity_setUp_ptr = setUp;
unit_tearDown_ptr = tearDown;
}
void UnityUnregisterSetupTearDown(void)
{
unity_setUp_ptr = NULL;
unit_tearDown_ptr = NULL;
}
UnityTestModule* UnityTestModuleFind(
UnityTestModule* modules,
size_t number_of_modules,
char* wantedModule)
{
for(size_t i = 0; i < number_of_modules; i++) {
if(strcmp(wantedModule, modules[i].name) == 0) {
return &modules[i];
}
}
return NULL;
}
void UnityTestModuleRunRequestedModules(
int number_of_requested_modules,
char* requested_modules_names[],
UnityTestModule* allModules,
size_t number_of_modules )
{
for(int i = 0; i < number_of_requested_modules; i++)
{
UnityTestModule* module = UnityTestModuleFind(allModules,
number_of_modules, requested_modules_names[i]);
if(module != NULL)
{
module->run_tests();
}
else
{
printf("Ignoring: could not find requested module: %s\n",
requested_modules_names[i]);
}
}
}
int UnityTestModuleRun(
int argc,
char * argv[],
UnityTestModule* allModules,
size_t number_of_modules)
{
UnityBegin();
bool moduleRequests = (argc > 1);
if(moduleRequests)
{
UnityTestModuleRunRequestedModules(argc-1, &argv[1],
allModules, number_of_modules);
}
else
{
for(int i = 0; i < number_of_modules; i++)
{
allModules[i].run_tests();
}
}
return UnityEnd();
}

View File

@@ -0,0 +1,31 @@
#ifndef UNITY_TEST_MODULE_H
#define UNITY_TEST_MODULE_H
#include <stddef.h>
typedef struct {
char name[24];
void(*run_tests)(void);
} UnityTestModule;
void UnityRegisterSetupTearDown( void(*setUp)(void), void(*tearDown)(void) );
void UnityUnregisterSetupTearDown(void);
UnityTestModule* UnityTestModuleFind(
UnityTestModule* modules,
size_t number_of_modules,
char* wantedModule);
void UnityTestModuleRunRequestedModules(
int number_of_requested_modules,
char* requested_modules_names[],
UnityTestModule* allModules,
size_t number_of_modules );
int UnityTestModuleRun(
int argc,
char * argv[],
UnityTestModule* allModules,
size_t number_of_modules);
#endif

View File

@@ -0,0 +1,6 @@
{
"challenge":{
"level":["knows"] ,
"durationHours": 4
}
}

View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include "calculations.h"
int main()
{
// from her your program starts
int variableWithAName = 14;
int variableWithAnotherName = 12;
int result = swap(&variableWithAName, &variableWithAnotherName);
if (result==0)
{
printf("succeeded\n");
}
else
{
printf("failed, the implementation is empty\n");
}
return (0);
}

View File

@@ -0,0 +1,15 @@
#include "calculations.h"
#include <stddef.h>
int swap(int* getal1, int* getal2)
{
if(getal1 == NULL || getal2==NULL)
{
return -1;
}
//TODO insert code here
return -1;
}

View File

@@ -0,0 +1,16 @@
#ifndef CALCULATIONS_H
#define CALCULATIONS_H
#include <stdint.h>
// return
// 0 : swap gelukt
//-1 : fout opgtreden
int swap(int* getal1, int* getal2);
// 0 : GELUKT
// -1 : er is iets mis gegaan
int sort (int array[]);
#endif

View File

@@ -0,0 +1,76 @@
/*
* auteur : Freddy Hurkmans
* datum : November 15th 2015
* code : C99
*/
#include "calculations.h"
#include "unity.h"
// I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number
#define MY_RUN_TEST(func) RUN_TEST(func, 0)
void setUp(void)
{
// This is run before EACH test
}
void tearDown(void)
{
// This is run after EACH test
}
void test_to_see_if_our_compiler_can_add_2_integers(void)
{
int result = 2;
TEST_ASSERT_EQUAL(result, 1+1);
}
void testswap()
{
int getal1 =15;
int getal2 = 20;
int *getalptr = NULL;
getalptr = &getal1;
int result = swap(getalptr,&getal2);
TEST_ASSERT_EQUAL(0,result);
TEST_ASSERT_EQUAL(20,getal1);
TEST_ASSERT_EQUAL(15,getal2);
getal1 =0;
getal2 = 0;
result = swap(&getal1,&getal2);
TEST_ASSERT_EQUAL(0,result);
TEST_ASSERT_EQUAL(0,getal1);
TEST_ASSERT_EQUAL(0,getal2);
}
void testswapptrnull()
{
int getal1 =15;
int getal2 = 20;
int result = swap(&getal1,NULL);
TEST_ASSERT_EQUAL(-1,result);
result = swap(NULL,&getal2);
TEST_ASSERT_EQUAL(-1,result);
}
int main (int argc, char * argv[])
{
UnityBegin();
MY_RUN_TEST(test_to_see_if_our_compiler_can_add_2_integers);
MY_RUN_TEST(testswap);
MY_RUN_TEST (testswapptrnull);
return UnityEnd();
}