[add unit tests for stableptr
John Meacham <john@repetae.net>**20120212031148
 Ignore-this: 17b41baeec806fb53ca2c10c6489097
] hunk ./rts/jhc_rts_header.h 29
-#include "rts/wsize.h"
+#include "sys/wsize.h"
hunk ./rts/rts/stableptr.c 29
+/*
+wptr_t c_castPtrToStablePtr(void *)
+void * c_castStablePtrToPtr(wptr_t)
+*/
+
hunk ./rts/test/Makefile 3
+CC=gcc
hunk ./rts/test/Makefile 6
+all: slab_test stableptr_test
+
hunk ./rts/test/Makefile 12
+
+test: all
+	./slab_test
+	./stableptr_test
+
+stableptr_test: stableptr_test.c seatest.c
addfile ./rts/test/seatest.c
hunk ./rts/test/seatest.c 1
+#include "seatest.h"
+#include <string.h>
+#ifdef WIN32
+#include <conio.h>
+#include "windows.h"
+#else
+unsigned int GetTickCount() { return 0;}
+void _getch( void ) { }
+#endif
+
+static int sea_tests_run = 0;
+static int sea_tests_passed = 0;
+static int sea_tests_failed = 0;
+static char* seatest_current_fixture;
+
+static void (*seatest_suite_setup_func)( void ) = 0;
+static void (*seatest_suite_teardown_func)( void ) = 0;
+static void (*seatest_fixture_setup)( void ) = 0;
+static void (*seatest_fixture_teardown)( void ) = 0;
+
+
+
+void suite_setup(void (*setup)( void ))
+{
+	seatest_suite_setup_func = setup;
+}
+void suite_teardown(void (*teardown)( void ))
+{
+	seatest_suite_teardown_func = teardown;
+}
+
+void seatest_suite_setup( void )
+{
+	if(seatest_suite_setup_func != 0) seatest_suite_setup_func();
+}
+
+void seatest_suite_teardown( void )
+{
+	if(seatest_suite_teardown_func != 0) seatest_suite_teardown_func();
+}
+
+void fixture_setup(void (*setup)( void ))
+{
+	seatest_fixture_setup = setup;
+}
+void fixture_teardown(void (*teardown)( void ))
+{
+	seatest_fixture_teardown = teardown;
+}
+
+void seatest_setup( void )
+{
+	if(seatest_fixture_setup != 0) seatest_fixture_setup();
+}
+
+void seatest_teardown( void )
+{
+	if(seatest_fixture_teardown != 0) seatest_fixture_teardown();
+}
+
+char* test_file_name(char* path)
+{
+	char* file = path + strlen(path);
+	while(file != path && *file!= '\\' ) file--;
+	if(*file == '\\') file++;
+	return file;
+}
+
+static int seatest_fixture_tests_run;
+static int seatest_fixture_tests_failed;
+
+void seatest_simple_test_result(int passed, char* reason, const char* function, unsigned int line)
+{
+	if (!passed)
+	{
+		printf("%-20s Line %-5d %s\r\n", function, line, reason );
+		sea_tests_failed++;
+	}
+	else
+	{
+		sea_tests_passed++;
+	}
+}
+
+void seatest_assert_true(int test, const char* function, unsigned int line)
+{
+	seatest_simple_test_result(test, "Should of been true", function, line);
+
+}
+
+void seatest_assert_false(int test, const char* function, unsigned int line)
+{
+	seatest_simple_test_result(!test, "Should of been false", function, line);
+}
+
+
+void seatest_assert_int_equal(int expected, int actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %d but was %d", expected, actual);
+	seatest_simple_test_result(expected==actual, s, function, line);
+}
+
+void seatest_assert_ulong_equal(unsigned long expected, unsigned long actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %lu but was %lu", expected, actual);
+	seatest_simple_test_result(expected==actual, s, function, line);
+}
+
+void seatest_assert_float_equal( float expected, float actual, float delta, const char* function, unsigned int line )
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	float result = expected-actual;
+	sprintf(s, "Expected %f but was %f", expected, actual);
+	if(result < 0.0) result = 0.0f - result;
+	seatest_simple_test_result( result <= delta, s, function, line);
+}
+
+void seatest_assert_double_equal( double expected, double actual, double delta, const char* function, unsigned int line )
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	double result = expected-actual;
+	sprintf(s, "Expected %f but was %f", expected, actual);
+	if(result < 0.0) result = 0.0 - result;
+	seatest_simple_test_result( result <= delta, s, function, line);
+}
+
+void seatest_assert_string_equal(char* expected, char* actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %s but was %s", expected, actual);
+	seatest_simple_test_result(strcmp(expected, actual)==0, s, function, line);
+}
+
+void seatest_assert_string_ends_with(char* expected, char* actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %s to end with %s", actual, expected);
+	seatest_simple_test_result(strcmp(expected, actual+(strlen(actual)-strlen(expected)))==0, s, function, line);
+}
+
+void seatest_assert_string_starts_with(char* expected, char* actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %s to start with %s", actual, expected);
+	seatest_simple_test_result(strncmp(expected, actual, strlen(expected))==0, s, function, line);
+}
+
+void seatest_assert_string_contains(char* expected, char* actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %s to be in %s", expected, actual);
+	seatest_simple_test_result(strstr(actual, expected)!=0, s, function, line);
+}
+
+void seatest_assert_string_doesnt_contain(char* expected, char* actual, const char* function, unsigned int line)
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "Expected %s not to have %s in it", actual, expected);
+	seatest_simple_test_result(strstr(actual, expected)==0, s, function, line);
+}
+
+void seatest_run_test(void)
+{
+	sea_tests_run++;
+}
+
+void seatest_header_printer(char* s, int length, char f)
+{
+	int l = strlen(s);
+	int d = (length- (l + 2)) / 2;
+	int i;
+	for(i = 0; i<d; i++) printf("%c",f);
+	printf(" %s ", s);
+	for(i = (d+l+2); i<length; i++) printf("%c",f);
+	printf("\r\n");
+}
+
+
+void seatest_test_fixture_start(char* filepath)
+{
+	seatest_current_fixture = test_file_name(filepath);
+	seatest_header_printer(seatest_current_fixture, 50, '-');
+	seatest_fixture_tests_failed = sea_tests_failed;
+	seatest_fixture_tests_run = sea_tests_run;
+	seatest_fixture_teardown = 0;
+	seatest_fixture_setup = 0;
+}
+
+void seatest_test_fixture_end()
+{
+	char s[SEATEST_PRINT_BUFFER_SIZE];
+	sprintf(s, "%d run  %d failed", sea_tests_run-seatest_fixture_tests_run, sea_tests_failed-seatest_fixture_tests_failed);
+	seatest_header_printer(s, 50, ' ');
+	printf("\r\n");
+}
+
+static char* seatest_fixture_filter = 0;
+static char* seatest_test_filter = 0;
+
+void fixture_filter(char* filter)
+{
+	seatest_fixture_filter = filter;
+}
+
+
+void test_filter(char* filter)
+{
+	seatest_test_filter = filter;
+}
+
+
+int seatest_should_run( char* fixture, char* test)
+{
+	int run = 1;
+	if(seatest_fixture_filter)
+	{
+		if(strncmp(seatest_fixture_filter, fixture, strlen(seatest_fixture_filter)) != 0) run = 0;
+	}
+	if(seatest_test_filter)
+	{
+		if(strncmp(seatest_test_filter, test, strlen(seatest_test_filter)) != 0) run = 0;
+	}
+	return run;
+}
+
+int run_tests(void (*tests)(void))
+{
+	unsigned long end;
+	unsigned long start = GetTickCount();
+	tests();
+	end = GetTickCount();
+	printf("\r\n\r\n==================SEATEST v%s====================\r\n\r\n", SEATEST_VERSION);
+	if (sea_tests_failed > 0) {
+		printf("                      Failed\r\n");
+	}
+	else {
+		printf("               ALL TESTS PASSED\r\n");
+	}
+	printf("                 %d tests run\r\n", sea_tests_run);
+	printf("                    in %lu ms\r\n",end - start);
+	printf("==================================================\r\n");
+
+	_getch();
+	return sea_tests_failed == 0;
+}
+
addfile ./rts/test/seatest.h
hunk ./rts/test/seatest.h 1
+#ifndef SEATEST_H
+#define SEATEST_H
+#include <stdio.h>
+
+/*
+Defines
+*/
+
+#define SEATEST_VERSION "0.5"
+#define SEATEST_PROJECT_HOME "http://code.google.com/p/seatest/"
+#define SEATEST_PRINT_BUFFER_SIZE 100000
+
+/*
+Declarations
+*/
+
+void seatest_test_fixture_start(char* filepath);
+void seatest_test_fixture_end( void );
+void seatest_simple_test_result(int passed, char* reason, const char* function, unsigned int line);
+void seatest_assert_true(int test, const char* function, unsigned int line);
+void seatest_assert_false(int test, const char* function, unsigned int line);
+void seatest_assert_int_equal(int expected, int actual, const char* function, unsigned int line);
+void seatest_assert_ulong_equal(unsigned long expected, unsigned long actual, const char* function, unsigned int line);
+void seatest_assert_float_equal(float expected, float actual, float delta, const char* function, unsigned int line);
+void seatest_assert_double_equal(double expected, double actual, double delta, const char* function, unsigned int line);
+void seatest_assert_string_equal(char* expected, char* actual, const char* function, unsigned int line);
+void seatest_assert_string_ends_with(char* expected, char* actual, const char* function, unsigned int line);
+void seatest_assert_string_starts_with(char* expected, char* actual, const char* function, unsigned int line);
+void seatest_assert_string_contains(char* expected, char* actual, const char* function, unsigned int line);
+void seatest_assert_string_doesnt_contain(char* expected, char* actual, const char* function, unsigned int line);
+int seatest_should_run( char* fixture, char* test);
+void seatest_run_test(void);
+void seatest_setup( void );
+void seatest_teardown( void );
+void seatest_suite_teardown( void );
+void seatest_suite_setup( void );
+
+/*
+Assert Macros
+*/
+
+#define assert_true(test) do { seatest_assert_true(test, __FUNCTION__, __LINE__); } while (0)
+#define assert_false(test) do {  seatest_assert_false(test, __FUNCTION__, __LINE__); } while (0)
+#define assert_int_equal(expected, actual) do {  seatest_assert_int_equal((int)(expected), (int)(actual), __FUNCTION__, __LINE__); } while (0)
+#define assert_ulong_equal(expected, actual) do {  seatest_assert_ulong_equal(expected, actual, __FUNCTION__, __LINE__); } while (0)
+#define assert_string_equal(expected, actual) do {  seatest_assert_string_equal(expected, actual, __FUNCTION__, __LINE__); } while (0)
+#define assert_n_array_equal(expected, actual, n) do { int seatest_count; for(seatest_count=0; seatest_count<n; seatest_count++) { char s_seatest[SEATEST_PRINT_BUFFER_SIZE]; sprintf(s_seatest,"Expected %d to be %d at position %d", actual[seatest_count], expected[seatest_count], seatest_count); seatest_simple_test_result((expected[seatest_count] == actual[seatest_count]), s_seatest, __FUNCTION__, __LINE__);} } while (0)
+#define assert_bit_set(bit_number, value) { seatest_simple_test_result(((1 << bit_number) & value), " Expected bit to be set" ,  __FUNCTION__, __LINE__); } while (0)
+#define assert_bit_not_set(bit_number, value) { seatest_simple_test_result(!((1 << bit_number) & value), " Expected bit not to to be set" ,  __FUNCTION__, __LINE__); } while (0)
+#define assert_bit_mask_matches(value, mask) { seatest_simple_test_result(((value & mask) == mask), " Expected all bits of mask to be set" ,  __FUNCTION__, __LINE__); } while (0)
+#define assert_fail(message) { seatest_simple_test_result(0, message,  __FUNCTION__, __LINE__); } while (0)
+#define assert_float_equal(expected, actual, delta) do {  seatest_assert_float_equal(expected, actual, delta, __FUNCTION__, __LINE__); } while (0)
+#define assert_double_equal(expected, actual, delta) do {  seatest_assert_double_equal(expected, actual, delta, __FUNCTION__, __LINE__); } while (0)
+#define assert_string_contains(expected, actual) do {  seatest_assert_string_contains(expected, actual, __FUNCTION__, __LINE__); } while (0)
+#define assert_string_doesnt_contain(expected, actual) do {  seatest_assert_string_doesnt_contain(expected, actual, __FUNCTION__, __LINE__); } while (0)
+#define assert_string_starts_with(expected, actual) do {  seatest_assert_string_starts_with(expected, actual, __FUNCTION__, __LINE__); } while (0)
+#define assert_string_ends_with(expected, actual) do {  seatest_assert_string_ends_with(expected, actual, __FUNCTION__, __LINE__); } while (0)
+
+/*
+Fixture / Test Management
+*/
+
+void fixture_setup(void (*setup)( void ));
+void fixture_teardown(void (*teardown)( void ));
+#define run_test(test) do { if(seatest_should_run(__FILE__, #test)) {seatest_suite_setup(); seatest_setup(); test(); seatest_teardown(); seatest_suite_teardown(); seatest_run_test();  }} while (0)
+#define test_fixture_start() do { seatest_test_fixture_start(__FILE__); } while (0)
+#define test_fixture_end() do { seatest_test_fixture_end();} while (0)
+void fixture_filter(char* filter);
+void test_filter(char* filter);
+
+int run_tests(void (*tests)(void));
+void suite_teardown(void (*teardown)( void ));
+void suite_setup(void (*setup)( void ));
+#endif
addfile ./rts/test/seatest_license.txt
hunk ./rts/test/seatest_license.txt 1
+ Copyright (c) 2010 Keith Nicholas
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
addfile ./rts/test/stableptr_test.c
hunk ./rts/test/stableptr_test.c 1
+char jhc_c_compile[] = "(compile)";
+char jhc_command[] = "(command)";
+char jhc_version[] = "(version)";
+
+#define JHC_VALGRIND 1
+
+#define _JHC_STANDALONE 0
+#define _JHC_GC _JHC_GC_JGC
+
+#include "jhc_rts_header.h"
+#include "jhc_jgc.h"
+#include "debug.c"
+#include "jhc_rts_alloc.c"
+#include "jhc_rts.c"
+#include "profile.c"
+#include "jhc_rts2.c"
+#include "slub.c"
+#include "jhc_jgc.c"
+#include "rts/stableptr.c"
+
+#include "seatest.h"
+
+static void jhc_hs_init(void) {}
+static const void * const nh_stuff[] = { NULL };
+
+#define SAMPLE_SPTR (sptr_t)TO_SPTR(P_VALUE,0xF0D0);
+
+int num_stableptrs(void) {
+        int count = 0;
+        struct StablePtr *sp;
+        LIST_FOREACH(sp, &root_StablePtrs, link)
+            count++;
+        return count;
+}
+
+bool in_stableptr_list(struct StablePtr *spi) {
+        int count = 0;
+        struct StablePtr *sp;
+        LIST_FOREACH(sp, &root_StablePtrs, link)
+            if (sp == spi)
+                    return true;
+        return false;
+}
+
+void stableptr_test(void) {
+        assert_int_equal(0, num_stableptrs());
+        sptr_t sptr = SAMPLE_SPTR;
+        wptr_t wptr = c_newStablePtr(sptr);
+        assert_int_equal(1, num_stableptrs());
+        assert_true(c_derefStablePtr(wptr) == sptr);
+        c_freeStablePtr(wptr);
+        assert_int_equal(0, num_stableptrs());
+}
+
+int main(int argc, const char *argv[])
+{
+        test_fixture_start();
+        run_test(stableptr_test);
+        test_fixture_end();
+        return 0;
+}