Tests are slightly not.
This commit is contained in:
parent
6f283c1d12
commit
d244cfbfe1
15
README.md
15
README.md
@ -1,16 +1,3 @@
|
||||
# SCL: Simple Calculator Language
|
||||
|
||||
Project structure:
|
||||
|
||||
```bash
|
||||
$ tree -d
|
||||
.
|
||||
├── build # Build directory; where binaries (except the main one) go.
|
||||
│ ├── obj # Project objects.
|
||||
│ └── test # Test build directory.
|
||||
│ └── obj # Test objects.
|
||||
├── src # Source files (*.c).
|
||||
│ └── include # Header files (*.h)
|
||||
└── test # Tests (file names match those in src/*.c).
|
||||
└── unity # Unity test framework files.
|
||||
```
|
||||
A rewrite of halk.
|
||||
|
@ -21,7 +21,7 @@ typedef struct {
|
||||
char* cchar; // The current character.
|
||||
Token** tokens; // The tokens produced.
|
||||
size_t ntokens; // The number of tokens.
|
||||
LexerState state; // What the lexxer is looking at.
|
||||
LexerState state; // What the lexer is looking at.
|
||||
} Lexer;
|
||||
|
||||
// Create a lexer.
|
||||
@ -42,4 +42,7 @@ void lexer_do_call(Lexer* lexer);
|
||||
// Convert text to tokens.
|
||||
void lexer_lex(Lexer* lexer);
|
||||
|
||||
// Add a token to the lexer.
|
||||
void lexer_add_token(Lexer* lexer, Token* token);
|
||||
|
||||
#endif
|
||||
|
17
src/lexer.c
17
src/lexer.c
@ -1,4 +1,6 @@
|
||||
#include "include/lexer.h"
|
||||
#include "include/token.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
Lexer* lexer_init(char* src) {
|
||||
Lexer* lexer = malloc(sizeof(Lexer));
|
||||
@ -20,7 +22,15 @@ void lexer_destroy(Lexer* lexer) {
|
||||
for (int i = 0; i < lexer->ntokens; token_destroy(lexer->tokens[i++]));
|
||||
}
|
||||
|
||||
void lexer_do_confused(Lexer* lexer) {}
|
||||
void lexer_do_confused(Lexer* lexer) {
|
||||
int c = atoi(lexer->cchar);
|
||||
|
||||
if (c) lexer_add_token(lexer, token_init(TOKEN_TYPE_NUMBER, lexer->cchar));
|
||||
else lexer_add_token(lexer, token_init(TOKEN_TYPE_CALL, lexer->cchar));
|
||||
}
|
||||
|
||||
void lexer_do_number(Lexer* lexer) {}
|
||||
void lexer_do_call(Lexer* lexer) {}
|
||||
|
||||
void lexer_lex(Lexer* lexer) {
|
||||
while (*lexer->cchar) {
|
||||
@ -32,3 +42,8 @@ void lexer_lex(Lexer* lexer) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lexer_add_token(Lexer* lexer, Token* token) {
|
||||
(void)reallocarray(lexer->tokens, lexer->ntokens++, sizeof(Token));
|
||||
lexer->tokens[lexer->ntokens-1] = token;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "unity/unity.h"
|
||||
#include "registry.h"
|
||||
#include "unity/unity.h"
|
||||
|
||||
void (*tests[30])();
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "unity/unity_internals.h"
|
||||
#include "registry.h"
|
||||
#include "unity/unity_internals.h"
|
||||
|
||||
#define TESTS_MAX 128
|
||||
|
||||
@ -10,10 +10,11 @@ void register_test(Test t) {
|
||||
if (test_count < TESTS_MAX) {
|
||||
tests[test_count] = t;
|
||||
test_count++;
|
||||
} else
|
||||
printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX);
|
||||
} else printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX);
|
||||
}
|
||||
|
||||
void run_all_tests() {
|
||||
for (int i = 0; i < test_count; i++) tests[i]();
|
||||
for (int i = 0; i < test_count; i++) {
|
||||
int res = tests[i]();
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,12 @@
|
||||
#define REGISTRY_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "unity/unity.h"
|
||||
|
||||
// Test functions neither consume nor return anything.
|
||||
typedef void (*Test)(void);
|
||||
typedef int (*Test)(void);
|
||||
|
||||
// Register a new test function.
|
||||
void register_test(Test t);
|
||||
void register_test(Test);
|
||||
|
||||
// Run all registered tests.
|
||||
void run_all_tests();
|
||||
|
24
test/token.c
24
test/token.c
@ -1,31 +1,19 @@
|
||||
#include <stdlib.h>
|
||||
#include "unity/unity.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "registry.h"
|
||||
#include "unity/unity_internals.h"
|
||||
#include "../src/include/token.h"
|
||||
#include "registry.h"
|
||||
|
||||
void test_token_init() {
|
||||
int test_token_init() {
|
||||
UNITY_BEGIN();
|
||||
char* s = malloc(sizeof("Hello, world!"));
|
||||
s = "Hello, world!";
|
||||
Token* t = token_init(TOKEN_TYPE_CALL, s);
|
||||
TEST_ASSERT_EQUAL(TOKEN_TYPE_CALL, t->type);
|
||||
TEST_ASSERT_EQUAL_STRING("Hello, world!", t->val);
|
||||
}
|
||||
|
||||
void test_token_destroy() {
|
||||
char* s = malloc(1 * sizeof(char));
|
||||
*s = 'h';
|
||||
Token* t = token_init(TOKEN_TYPE_CALL, s);
|
||||
token_destroy(t);
|
||||
}
|
||||
|
||||
void token_test() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_token_init);
|
||||
UNITY_END();
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
__attribute__((constructor)) void register_tests_token() {
|
||||
register_test(token_test);
|
||||
register_test(test_token_init);
|
||||
}
|
||||
|
17
test/util.c
17
test/util.c
@ -1,19 +1,14 @@
|
||||
#include "unity/unity.h"
|
||||
#include "registry.h"
|
||||
#include "unity/unity_internals.h"
|
||||
#include "../src/include/util.h"
|
||||
#include "registry.h"
|
||||
#include "unity/unity.h"
|
||||
|
||||
void test_is_even() {
|
||||
int test_is_even() {
|
||||
UNITY_BEGIN();
|
||||
TEST_ASSERT_EQUAL(0, is_even(1));
|
||||
TEST_ASSERT_EQUAL(1, is_even(2));
|
||||
}
|
||||
|
||||
void util_test() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_is_even);
|
||||
UNITY_END();
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
__attribute__((constructor)) void register_tests_util() {
|
||||
register_test(util_test);
|
||||
register_test(test_is_even);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user