Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
cxxtestbuilder_path = '../../build_tools/SCons/cxxtest.py'
cxxtest_path = '../..'
# First a bit of python magic to make the CxxTestBuilder available
# without having to copy it into a particular path.
# for nicer examples you *should* use, see the cxxtest builder tests in the
# build_tools/SCons/test directory.
import imp
cxxtest = imp.load_source('cxxtest', cxxtestbuilder_path)
# First build the 'real' library, when working on an embedded system
# this may involve a cross compiler.
env = Environment()
env.BuildDir('build/embedded_platform', 'src')
env.Append(CPPPATH=['include'])
libtested = env.StaticLibrary('build/embedded_platform/tested',
env.Glob('build/embedded_platform/*.c'))
# Now create a seperate build environment for the tests so we can keep any
# options that are specific to testing seperate from the 'production' build
# environment. For simplicity I am just copying the production environment.
# If we are cross compiling for the "real" library, then this
# environement might be using the normal compiler.
env_test = env.Clone()
# Add the CxxTestBuilder to our testing build environment.
cxxtest.generate(env_test, CXXTEST_INSTALL_DIR = cxxtest_path)
# If we were working with an embedded platform we may want to create a
# seperate version of our library that runs on our development box in
# order to do our initial unit testing. This version may also include
# any special preprocessor defines needed for testing e.g. -DTESTING
env_test.BuildDir('build/dev_platform', 'src')
env_test.BuildDir('build/tests', 'tests')
lib_to_test = env_test.StaticLibrary('build/dev_platform/tested',
env.Glob('build/dev_platform/*.c'))
env_test.Append(LIBS=lib_to_test)
env_test.CxxTest(env_test.Glob('tests/*.h'))

View File

@@ -0,0 +1,27 @@
#ifndef STACK_H
#define STACK_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct stack_t {
int size;
int* vals;
int capacity;
} stack_t;
stack_t* stack_create();
void stack_free(stack_t* stack);
int stack_size(stack_t* stack);
void stack_push(stack_t* stack, int val);
int stack_pop(stack_t* stack);
int stack_peak(stack_t* stack);
int stack_capacity(stack_t* stack);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,48 @@
#include <stack.h>
#include <stdlib.h>
stack_t* stack_create() {
stack_t* retVal = malloc(sizeof(stack_t));
retVal->size = 0;
retVal->capacity = 10;
retVal->vals = malloc(retVal->capacity*sizeof(int));
return retVal;
}
void stack_free(stack_t* stack) {
free(stack->vals);
free(stack);
}
int stack_size(stack_t* stack) {
return stack->size;
}
void stack_push(stack_t* stack, int val) {
if(stack->size == stack->capacity) {
stack->capacity *= 2;
stack->vals = realloc(stack->vals, stack->capacity*sizeof(int));
}
stack->vals[stack->size++] = val;
}
int stack_pop(stack_t* stack) {
if (stack->size >= 1)
return stack->vals[--stack->size];
else
return 0;
}
int stack_peak(stack_t* stack) {
if (stack->size >= 1)
return stack->vals[stack->size-1];
else
return 0;
}
int stack_capacity(stack_t* stack) {
return stack->capacity;
}

View File

@@ -0,0 +1,71 @@
#ifndef STACK_TEST_H
#define STACK_TEST_H
#include <cxxtest/TestSuite.h>
#include <stack.h>
class stack_test : public CxxTest::TestSuite
{
private:
stack_t* stack;
public:
void setUp() {
stack = stack_create();
}
void tearDown() {
stack_free(stack);
}
void test_create_stack() {
TS_ASSERT_DIFFERS((stack_t*)0, stack);
}
void test_new_stack_is_empty() {
TS_ASSERT_EQUALS(0, stack_size(stack));
}
void test_one_push_add_one_to_size() {
stack_push(stack, 1);
TS_ASSERT_EQUALS(1, stack_size(stack));
}
void test_push_pop_doesnt_change_size() {
stack_push(stack, 1);
(void)stack_pop(stack);
TS_ASSERT_EQUALS(0, stack_size(stack));
}
void test_peak_after_push() {
stack_push(stack, 1);
TS_ASSERT_EQUALS(1, stack_peak(stack))
}
void test_initial_capacity_is_positive() {
TS_ASSERT(stack_capacity(stack) > 0);
}
void test_pop_on_empty() {
TS_ASSERT_EQUALS(0, stack_pop(stack));
TS_ASSERT_EQUALS(0, stack_size(stack));
}
void test_peak_on_empty() {
TS_ASSERT_EQUALS(0, stack_peak(stack));
}
void test_capacity_gte_size() {
TS_ASSERT_LESS_THAN_EQUALS(stack_size(stack), stack_capacity(stack));
int init_capacity = stack_capacity(stack);
for (int i=0; i < init_capacity + 1; i++) {
stack_push(stack, i);
}
TS_ASSERT_LESS_THAN_EQUALS(stack_size(stack), stack_capacity(stack));
}
};
#endif // STACK_TEST_H