From 69b6894f6b594052347b834176ebeb2b2a9e542f Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 18 Nov 2018 13:56:27 -0500 Subject: [PATCH] Add some tests for C api and code. --- Makefile | 22 +++++++++++++-- ctest/array_test.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ ctest/buffer_test.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 ctest/array_test.c create mode 100644 ctest/buffer_test.c diff --git a/Makefile b/Makefile index 137cea5d..76bdedad 100644 --- a/Makefile +++ b/Makefile @@ -132,6 +132,16 @@ $(JANET_EMTARGET): $(JANET_EMOBJECTS) ##### Testing ##### ################### +TEST_SOURCES=$(wildcard ctest/*.c) +TEST_OBJECTS=$(patsubst %.c,%.o,$(TEST_SOURCES)) +TEST_PROGRAMS=$(patsubst %.c,%.out,$(TEST_SOURCES)) + +ctest/%.o: ctest/%.c $(JANET_HEADERS) + $(CC) $(CFLAGS) -o $@ -c $< + +ctest/%.out: ctest/%.o $(JANET_CORE_OBJECTS) + $(CC) $(CFLAGS) -o $@ $^ $(CLIBS) + repl: $(JANET_TARGET) ./$(JANET_TARGET) @@ -141,12 +151,16 @@ debug: $(JANET_TARGET) valgrind: $(JANET_TARGET) valgrind --leak-check=full -v ./$(JANET_TARGET) -test: $(JANET_TARGET) +test: $(JANET_TARGET) $(TEST_PROGRAMS) + ctest/array_test.out + ctest/buffer_test.out ./$(JANET_TARGET) test/suite0.janet ./$(JANET_TARGET) test/suite1.janet ./$(JANET_TARGET) test/suite2.janet -valtest: $(JANET_TARGET) +valtest: $(JANET_TARGET) $(TEST_PROGRAMS) + valgrind --leak-check=full -v ctest/array_test.out + valgrind --leak-check=full -v ctest/buffer_test.out valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite0.janet valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite1.janet valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite2.janet @@ -170,6 +184,7 @@ clean-natives: clean: -rm $(JANET_TARGET) -rm $(JANET_LIBRARY) + -rm ctest/*.o ctest/*.out -rm src/**/*.o src/**/*.bc vgcore.* *.js *.wasm *.html -rm $(JANET_GENERATED_HEADERS) @@ -195,4 +210,5 @@ uninstall: -rm -rf $(INCLUDEDIR) $(LDCONFIG) -.PHONY: clean install repl debug valgrind test valtest install uninstall +.PHONY: clean install repl debug valgrind test valtest install uninstall \ + $(TEST_PROGRAM_PHONIES) $(TEST_PROGRAM_VALPHONIES) diff --git a/ctest/array_test.c b/ctest/array_test.c new file mode 100644 index 00000000..f42d591a --- /dev/null +++ b/ctest/array_test.c @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2018 Calvin Rose +* +* 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. +*/ + +#include +#include + +int main() { + + int i; + JanetArray *array1, *array2; + + janet_init(); + + array1 = janet_array(10); + array2 = janet_array(0); + + janet_array_push(array1, janet_cstringv("one")); + janet_array_push(array1, janet_cstringv("two")); + janet_array_push(array1, janet_cstringv("three")); + janet_array_push(array1, janet_cstringv("four")); + janet_array_push(array1, janet_cstringv("five")); + janet_array_push(array1, janet_cstringv("six")); + janet_array_push(array1, janet_cstringv("seven")); + + assert(array1->count == 7); + assert(array1->capacity >= 7); + assert(janet_equals(array1->data[0], janet_cstringv("one"))); + + janet_array_push(array2, janet_cstringv("one")); + janet_array_push(array2, janet_cstringv("two")); + janet_array_push(array2, janet_cstringv("three")); + janet_array_push(array2, janet_cstringv("four")); + janet_array_push(array2, janet_cstringv("five")); + janet_array_push(array2, janet_cstringv("six")); + janet_array_push(array2, janet_cstringv("seven")); + + for (i = 0; i < array2->count; i++) { + assert(janet_equals(array1->data[i], array2->data[i])); + } + + janet_array_pop(array1); + janet_array_pop(array1); + + assert(array1->count == 5); + + janet_deinit(); + + return 0; +} diff --git a/ctest/buffer_test.c b/ctest/buffer_test.c new file mode 100644 index 00000000..76514da5 --- /dev/null +++ b/ctest/buffer_test.c @@ -0,0 +1,64 @@ +/* +* Copyright (c) 2018 Calvin Rose +* +* 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. +*/ + +#include +#include + +int main() { + + int i; + JanetBuffer *buffer1, *buffer2; + + janet_init(); + + buffer1 = janet_buffer(100); + buffer2 = janet_buffer(0); + + janet_buffer_push_cstring(buffer1, "hello, world!\n"); + + janet_buffer_push_u8(buffer2, 'h'); + janet_buffer_push_u8(buffer2, 'e'); + janet_buffer_push_u8(buffer2, 'l'); + janet_buffer_push_u8(buffer2, 'l'); + janet_buffer_push_u8(buffer2, 'o'); + janet_buffer_push_u8(buffer2, ','); + janet_buffer_push_u8(buffer2, ' '); + janet_buffer_push_u8(buffer2, 'w'); + janet_buffer_push_u8(buffer2, 'o'); + janet_buffer_push_u8(buffer2, 'r'); + janet_buffer_push_u8(buffer2, 'l'); + janet_buffer_push_u8(buffer2, 'd'); + janet_buffer_push_u8(buffer2, '!'); + janet_buffer_push_u8(buffer2, '\n'); + + assert(buffer1->count == buffer2->count); + assert(buffer1->capacity >= buffer1->count); + assert(buffer2->capacity >= buffer2->count); + + for (i = 0; i < buffer1->count; i++) { + assert(buffer1->data[i] == buffer2->data[i]); + } + + janet_deinit(); + + return 0; +}