aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/main.c80
-rw-r--r--tests/utf8bom.amal1
2 files changed, 30 insertions, 51 deletions
diff --git a/tests/main.c b/tests/main.c
index 94f2d32..9b1e40a 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <unistd.h>
+static int num_threads = 0;
static int num_successful_tests = 0;
static int num_tests_run = 0;
@@ -26,7 +27,7 @@ static CHECK_RESULT int test_hash_map() {
unsigned char i;
return_if_error(scoped_allocator_init(&scoped_allocator));
- cleanup_if_error(hash_map_init(&hash_map, &scoped_allocator, sizeof(int), hash_compare_string, amal_hash_string));
+ cleanup_if_error(hash_map_init(&hash_map, &scoped_allocator, sizeof(int), hash_map_compare_string, amal_hash_string));
value = 34;
return_if_error(hash_map_insert(&hash_map, create_buffer_view("hello", 5), &value));
@@ -131,48 +132,29 @@ static CHECK_RESULT int get_thread_count_env_var(int *thread_count) {
}
static void test_load(const char *filepath) {
- amal_compiler compiler;
amal_compiler_options options;
+ amal_program program;
char *full_path;
int result;
- int num_threads;
-
- result = get_thread_count_env_var(&num_threads);
- if(result != 0)
- num_threads = 0;
-
- if(num_threads < 0) {
- amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)");
- exit(1);
- }
amal_compiler_options_init(&options);
options.num_threads = num_threads;
++num_tests_run;
full_path = get_full_path(filepath);
- /*
- options.error_callback = error_callback;
- ErrorUnexpectedData data;
- data.filepath = get_full_path(filepath);
- options.error_callback_userdata = &data;
- */
-
- result = amal_compiler_init(&compiler, &options);
- if(result != AMAL_COMPILER_OK) {
- fprintf(stderr, "Failed to initialize compiler to test %s, error code: %d\n", full_path, result);
- FAIL_TEST(full_path);
- }
- result = amal_compiler_load_file(&compiler, filepath);
+ amal_program_init(&program);
+ result = amal_compiler_load_file(&options, &program, filepath);
if(result != AMAL_COMPILER_OK) {
fprintf(stderr, "Failed to load file %s, result: %d\n", full_path, result);
FAIL_TEST(full_path);
}
- if(amal_compiler_deinit(&compiler) != 0) {
- fprintf(stderr, "Failed to deinitialize compiler for test %s\n", full_path);
+ result = amal_program_run(&program);
+ if(result != 0) {
+ fprintf(stderr, "Failed to run the program %s, result: %d\n", full_path, result);
FAIL_TEST(full_path);
}
+ amal_program_deinit(&program);
fprintf(stderr, "Test succeeded as expected: %s\n", full_path);
++num_successful_tests;
@@ -180,20 +162,10 @@ static void test_load(const char *filepath) {
}
static void test_load_error(const char *filepath, const char *expected_error) {
- amal_compiler compiler;
amal_compiler_options options;
+ amal_program program;
ErrorExpectedData expected_data;
int result;
- int num_threads;
-
- result = get_thread_count_env_var(&num_threads);
- if(result != 0)
- num_threads = 0;
-
- if(num_threads < 0) {
- amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)");
- exit(1);
- }
amal_compiler_options_init(&options);
options.num_threads = num_threads;
@@ -205,22 +177,13 @@ static void test_load_error(const char *filepath, const char *expected_error) {
expected_data.got_expected_error = bool_false;
options.error_callback_userdata = &expected_data;
- result = amal_compiler_init(&compiler, &options);
- if(result != AMAL_COMPILER_OK) {
- fprintf(stderr, "Failed to initialize compiler, error code: %d\n", result);
- FAIL_TEST(expected_data.filepath);
- }
-
- result = amal_compiler_load_file(&compiler, filepath);
+ amal_program_init(&program);
+ result = amal_compiler_load_file(&options, &program, filepath);
if(result == AMAL_COMPILER_OK) {
fprintf(stderr, "Expected to fail loading file\n");
FAIL_TEST(expected_data.filepath);
}
-
- if(amal_compiler_deinit(&compiler) != 0) {
- fprintf(stderr, "Failed to deinitialize compiler.\n");
- FAIL_TEST(expected_data.filepath);
- }
+ amal_program_deinit(&program);
if(!expected_data.got_expected_error) {
fprintf(stderr, "Didn't get expected error message:\n%s\n", expected_error);
@@ -235,17 +198,30 @@ static void test_load_error(const char *filepath, const char *expected_error) {
/* TODO: Restrict variables in global scope to const */
int main(int argc, char **argv) {
+ int result;
+ result = get_thread_count_env_var(&num_threads);
+ if(result != 0)
+ num_threads = 0;
+
+ if(num_threads < 0) {
+ amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)");
+ exit(1);
+ }
+
return_if_error(test_hash_map());
/* Run all tests */
if(argc == 1) {
test_load("tests/main.amal");
+ test_load("tests/utf8bom.amal");
+ test_load("tests/bytecode.amal");
+
test_load_error("tests/errors/duplicate_declaration.amal",
"2:7: error: Variable with the name main was declared twice in the same scope\n"
"const main = fn {}\n"
" ^\n");
test_load_error("tests/errors/pub_in_closure.amal",
- "2:5: error: Only declarations in structs can be public\n"
+ "2:5: error: Only declarations in global structs can be public\n"
" pub const num = 45;\n"
" ^\n");
test_load_error("tests/errors/closure_no_lhs.amal",
@@ -262,10 +238,12 @@ int main(int argc, char **argv) {
fprintf(stderr, "##### %d/%d tests succeeded #####\n", num_successful_tests, num_tests_run);
} else {
fprintf(stderr, "usage: test [test-file-path]\n");
+ fprintf(stderr, "If you run test without any files then all tests will run.\n");
fprintf(stderr, "examples:\n");
fprintf(stderr, " test\n");
fprintf(stderr, " test tests/main.amal\n");
exit(1);
}
+
return 0;
} \ No newline at end of file
diff --git a/tests/utf8bom.amal b/tests/utf8bom.amal
new file mode 100644
index 0000000..673e77f
--- /dev/null
+++ b/tests/utf8bom.amal
@@ -0,0 +1 @@
+// This file has utf-8 BOM