Skip to content

Commit

Permalink
#97 Added unit test framework and a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Mar 16, 2016
1 parent d177bde commit 66aff47
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ version.c
/cf2.*
/cf1.*
*.gch

/generated-test/**
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "vendor/libdw1000"]
path = vendor/libdw1000
url = https://github.com/bitcraze/libdw1000
[submodule "vendor/unity"]
path = vendor/unity
url = https://github.com/throwtheswitch/unity.git
[submodule "vendor/cmock"]
path = vendor/cmock
url = https://github.com/throwtheswitch/cmock.git
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,27 @@ halt : Halt the target using OpenOCD
reset : Reset the target using OpenOCD
openocd : Launch OpenOCD
```

# Unit testing

## Dependencies

Frameworks for unit testing are pulled in as git submodules.

The testing framework uses ruby and rake to generate and run code.

To minimize the need for installations and configuration, use the docker builder
image (bitcraze/builder) that contains all tools needed. All scripts in the
tools/build directory are intended to be run in the image. The
[toolbelt](https://wiki.bitcraze.io/projects:dockerbuilderimage:index) makes it
easy to run the tool scripts.

### Running unit tests

With the environment set up locally

rake

with the docker builder image and the toolbelt

tb test
34 changes: 34 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Rakefile used for running unit tests

HERE = File.expand_path(File.dirname(__FILE__)) + '/'

require 'rake'
require 'rake/clean'
require 'rake/testtask'
require './tools/test/rakefile_helper'

include RakefileHelpers

# Load default configuration, for now
DEFAULT_CONFIG_FILE = './tools/test/gcc.yml'
configure_toolchain(DEFAULT_CONFIG_FILE)

task :unit do
run_tests(get_unit_test_files)
end

desc "Generate test summary"
task :summary do
report_summary
end

desc "Build and test Unity"
task :all => [:clean, :unit, :summary]
task :default => [:clobber, :all]
task :ci => [:default]
task :cruise => [:default]

desc "Load configuration"
task :config, :config_file do |t, args|
configure_toolchain(args[:config_file])
end
Empty file added generated-test/build/.gitkeep
Empty file.
Empty file added generated-test/mocks/.gitkeep
Empty file.
138 changes: 138 additions & 0 deletions test/utils/src/TestNum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "unity.h"
#include "num.h"


void testThatLimitUint16NotLimitInRange() {
// Fixture
int32_t value = 4711;
uint16_t expected = 4711;

// Test
uint16_t actual = limitUint16(value);

// Assert
TEST_ASSERT_EQUAL_UINT16(expected, actual);
}


void testThatLimitUint16LimitZero() {
// Fixture
int32_t value = -100;
uint16_t expected = 0;

// Test
uint16_t actual = limitUint16(value);

// Assert
TEST_ASSERT_EQUAL_UINT16(expected, actual);
}


void testThatLimitUint16LimitMax() {
// Fixture
int32_t value = UINT16_MAX + 100;
uint16_t expected = UINT16_MAX;

// Test
uint16_t actual = limitUint16(value);

// Assert
TEST_ASSERT_EQUAL_UINT16(expected, actual);
}


void testThatConstrainDoesNotLimitInRange() {
// Fixture
float value = 3.45;
float expected = value;

// Test
float actual = constrain(value, 0.0, 100.0);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatConstrainLimitsMin() {
// Fixture
float value = -10.0;
float min = 3.45;
float expected = min;

// Test
float actual = constrain(value, min, 100.0);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatConstrainLimitsMax() {
// Fixture
float value = 10.0;
float max = 3.45;
float expected = max;

// Test
float actual = constrain(value, -100.0, max);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatDeadbandInDeadbandPositive() {
// Fixture
float value = 0.5;
float threshold = 1.0;
float expected = 0.0;

// Test
float actual = deadband(value, threshold);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatDeadbandInDeadbandNegative() {
// Fixture
float value = -0.5;
float threshold = 1.0;
float expected = 0.0;

// Test
float actual = deadband(value, threshold);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatDeadbandOutsideDeadbandPositive() {
// Fixture
float value = 2.5;
float threshold = 1.0;
float expected = 1.5;

// Test
float actual = deadband(value, threshold);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}


void testThatDeadbandOutsideDeadbandNegative() {
// Fixture
float value = -2.5;
float threshold = 1.0;
float expected = -1.5;

// Test
float actual = deadband(value, threshold);

// Assert
TEST_ASSERT_EQUAL_FLOAT(expected, actual);
}
3 changes: 2 additions & 1 deletion tools/build/build
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ set -e

scriptDir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

make --directory=${scriptDir}/../.. "${@}"
${scriptDir}/test
${scriptDir}/compile "${@}"
6 changes: 6 additions & 0 deletions tools/build/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e

scriptDir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

make --directory=${scriptDir}/../.. "${@}"
6 changes: 6 additions & 0 deletions tools/build/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e

scriptDir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

rake -f ${scriptDir}/../../Rakefile
66 changes: 66 additions & 0 deletions tools/test/gcc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
compiler:
path: gcc
source_path: &source_path 'src/'
unit_tests_path: &unit_tests_path 'test/**/'
mocks_path: &mocks_path 'generated-test/mocks/'
build_path: &build_path 'generated-test/build/'
options:
- '-c'
- '-Wall'
- '-Wextra'
- '-Wunused-parameter'
- '-Wno-address'
- '-std=c11'
- '-pedantic'
- '-O0'
includes:
prefix: '-I'
items:
- *source_path
- *unit_tests_path
- *mocks_path
- 'vendor/unity/src/'
- 'vendor/cmock/src/'
- 'src/utils/interface/'
- 'src/utils/src/'
defines:
prefix: '-D'
items:
- CMOCK
- 'UNITY_SUPPORT_64'
- 'HSI48_VALUE="((uint32_t)48000000)"'
- 'STM32F072xB'
object_files:
prefix: '-o'
extension: '.o'
destination: *build_path

linker:
path: gcc
options:
- -lm
includes:
prefix: '-I'
object_files:
path: *build_path
extension: '.o'
bin_files:
prefix: '-o'
extension: '.exe'
destination: *build_path

unsupported:
- out_of_memory
- unity_64bit_support
- callingconv

colour: true

:cmock:
:plugins:
- :ignore_arg
- :array
- :callback
:mock_path: *mocks_path
:mock_prefix: mock_
Loading

0 comments on commit 66aff47

Please sign in to comment.