desc "Clean all generated files and directories"
task(:clean) { Rscons.clean }
+desc "Clobber all generated files and directories"
+task(:clobber) { FileUtils.rm_rf('build') }
+
#include "exn.h"
#include <stdio.h>
-#ifndef EXN_MAX_NUM_HANDLERS
-#define EXN_MAX_NUM_HANDLERS (8)
+#ifdef TESTING
+extern void test_exit(int);
+#define exit(status) test_exit(status)
#endif
+//#ifndef EXN_MAX_NUM_HANDLERS
+//#define EXN_MAX_NUM_HANDLERS (8)
+//#endif
+
+typedef struct exn_stack_t {
+ struct exn_stack_t* p_next;
+ exn_handler_t handler;
+} exn_stack_t;
+
DEFINE_EXCEPTION(RuntimeException, NULL);
DEFINE_EXCEPTION(NullPointerException, &RuntimeException);
DEFINE_EXCEPTION(AssertionException, &RuntimeException);
static bool Exn_Handled = true;
static const exn_t* Exn_Current = NULL;
-static int Exn_Num_Handlers = 0;
-static exn_handler_t Exn_Handlers[EXN_MAX_NUM_HANDLERS];
+static exn_stack_t* Exn_Handlers = NULL;
static void exn_uncaught(const exn_t* p_exn) {
(void)p_exn;
exit(1);
}
+static void exn_pop(void) {
+ exn_stack_t* p_prev = Exn_Handlers;
+ Exn_Handlers = p_prev->p_next;
+ free(p_prev);
+}
+
void exn_prep(void) {
- if((Exn_Num_Handlers+1) > EXN_MAX_NUM_HANDLERS)
- exn_throw(&RuntimeException);
- Exn_Handlers[Exn_Num_Handlers].state = EXN_BEGIN;
- Exn_Num_Handlers++;
+ exn_stack_t* p_prev = Exn_Handlers;
+ Exn_Handlers = (exn_stack_t*)malloc(sizeof(exn_stack_t));
+ Exn_Handlers->p_next = p_prev;
+ Exn_Handlers->handler.state = EXN_BEGIN;
Exn_Handled = true;
}
break;
case EXN_DONE:
- Exn_Num_Handlers--;
+ exn_pop();
ret = false;
break;
}
void exn_throw(const exn_t* p_type) {
- if (Exn_Num_Handlers == 0) {
+ if (Exn_Handlers == NULL) {
exn_uncaught(Exn_Current);
} else {
Exn_Current = p_type;
}
void exn_rethrow(void) {
- Exn_Num_Handlers--;
+ printf("rethrowing 1 %p\n", Exn_Handlers);
+ exn_pop();
+ printf("rethrowing 2 %p\n", Exn_Handlers);
exn_throw(Exn_Current);
}
}
exn_handler_t* exn_handler(void) {
- return &(Exn_Handlers[Exn_Num_Handlers-1]);
+ return (NULL != Exn_Handlers) ? &(Exn_Handlers->handler) : NULL;
}
void exn_assert(bool expr) {
exn_state_t state;
} exn_handler_t;
+//typedef void (*)(const exn_t* p_exn);
+
#define DECLARE_EXCEPTION(exname) \
extern const exn_t exname
size_t Num_Allocations = 0;
#endif
-static void summarize_leaks(void) {
+#ifndef TESTING
+static
+#endif
+void summarize_leaks(void) {
#if (LEAK_DETECT_LEVEL == 2)
bool leak_detected = false;
block_t* p_curr = Live_Blocks;
rbt_node_t* ret = NULL;
if(node){
int c = tree->comp(value, node->contents);
- if(c == 0) ret = node;
+ if (c < 0) ret = rbt_lookup_node(tree, node->left, value);
else if(c > 0) ret = rbt_lookup_node(tree, node->right, value);
- else if(c < 0) ret = rbt_lookup_node(tree, node->left, value);
+ else ret = node;
}
return ret;
}
RUN_TEST_SUITE(String);
RUN_TEST_SUITE(RBT);
RUN_TEST_SUITE(Exn);
+ RUN_TEST_SUITE(Mem);
return PRINT_TEST_RESULTS();
}
{
buf_t* buf = buf_new(3);
buf_write( buf, mem_box(0x1234) );
- buf_write( buf, mem_box(0x1235) );
+ buf_write( buf, NULL );
buf_write( buf, mem_box(0x1236) );
buf_clear( buf );
CHECK( buf->reads == 0 );
// File To Test
#include "exn.h"
-static void test_setup(void) { }
+static int Exit_Status = 0;
+void test_exit(int status) {
+ Exit_Status = status;
+ if (NULL != exn_handler())
+ exn_handler()->state = EXN_DONE;
+}
+
+static void test_setup(void) {
+ Exit_Status = 0;
+}
//-----------------------------------------------------------------------------
// Begin Unit Tests
catch(AssertionException) { counter--; }
CHECK(counter == -1);
}
+
+ //-------------------------------------------------------------------------
+ // Test extraordinary conditions
+ //-------------------------------------------------------------------------
+ TEST(Verify_an_uncaught_exception_terminates_the_program)
+ {
+ throw(AssertionException);
+ CHECK( Exit_Status == 1 );
+ }
+
+ TEST(Verify_an_invalid_exception_state_terminates_the_program)
+ {
+ try {
+ exn_handler()->state = EXN_DONE+1;
+ exn_process();
+ }
+ CHECK( Exit_Status == 1 );
+ }
}
--- /dev/null
+// Unit Test Framework Includes
+#include "test.h"
+
+// File To Test
+#include "mem.h"
+
+static void test_setup(void) { }
+
+//-----------------------------------------------------------------------------
+// Begin Unit Tests
+//-----------------------------------------------------------------------------
+TEST_SUITE(Mem) {
+ TEST(Verify_mem_can_summarize_leaks)
+ {
+ void* p_obj = mem_allocate(sizeof(4),NULL);
+ extern void summarize_leaks(void);
+ summarize_leaks();
+ mem_release( p_obj );
+ }
+}