1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-15 21:24:48 +00:00

Add some tests for C api and code.

This commit is contained in:
Calvin Rose 2018-11-18 13:56:27 -05:00
parent 7ff74c8ac9
commit 69b6894f6b
3 changed files with 151 additions and 3 deletions

View File

@ -132,6 +132,16 @@ $(JANET_EMTARGET): $(JANET_EMOBJECTS)
##### Testing ##### ##### 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) repl: $(JANET_TARGET)
./$(JANET_TARGET) ./$(JANET_TARGET)
@ -141,12 +151,16 @@ debug: $(JANET_TARGET)
valgrind: $(JANET_TARGET) valgrind: $(JANET_TARGET)
valgrind --leak-check=full -v ./$(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/suite0.janet
./$(JANET_TARGET) test/suite1.janet ./$(JANET_TARGET) test/suite1.janet
./$(JANET_TARGET) test/suite2.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/suite0.janet
valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite1.janet valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite1.janet
valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite2.janet valgrind --leak-check=full -v ./$(JANET_TARGET) test/suite2.janet
@ -170,6 +184,7 @@ clean-natives:
clean: clean:
-rm $(JANET_TARGET) -rm $(JANET_TARGET)
-rm $(JANET_LIBRARY) -rm $(JANET_LIBRARY)
-rm ctest/*.o ctest/*.out
-rm src/**/*.o src/**/*.bc vgcore.* *.js *.wasm *.html -rm src/**/*.o src/**/*.bc vgcore.* *.js *.wasm *.html
-rm $(JANET_GENERATED_HEADERS) -rm $(JANET_GENERATED_HEADERS)
@ -195,4 +210,5 @@ uninstall:
-rm -rf $(INCLUDEDIR) -rm -rf $(INCLUDEDIR)
$(LDCONFIG) $(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)

68
ctest/array_test.c Normal file
View File

@ -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 <janet/janet.h>
#include <assert.h>
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;
}

64
ctest/buffer_test.c Normal file
View File

@ -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 <janet/janet.h>
#include <assert.h>
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;
}