Implement number evaluation
This commit is contained in:
parent
3bc640b656
commit
29deffdce9
8
lib/evaluator.c
Normal file
8
lib/evaluator.c
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "evaluator.h"
|
||||||
|
|
||||||
|
int evaluate(const expression_t *expression)
|
||||||
|
{
|
||||||
|
if (expression->is_number)
|
||||||
|
return expression->number;
|
||||||
|
return 0;
|
||||||
|
}
|
8
lib/include/evaluator.h
Normal file
8
lib/include/evaluator.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef EVALUATOR_H
|
||||||
|
#define EVALUATOR_H
|
||||||
|
|
||||||
|
#include "expression.h"
|
||||||
|
|
||||||
|
int evaluate(const expression_t *expression);
|
||||||
|
|
||||||
|
#endif
|
@ -7,12 +7,16 @@ CFLAGS="$CFLAGS -Ilib/include"
|
|||||||
mkdir -p build
|
mkdir -p build
|
||||||
|
|
||||||
# Build library
|
# Build library
|
||||||
|
clang $CFLAGS -c -o build/evaluator.o lib/evaluator.c
|
||||||
clang $CFLAGS -c -o build/memory_pool.o lib/memory_pool.c
|
clang $CFLAGS -c -o build/memory_pool.o lib/memory_pool.c
|
||||||
clang $CFLAGS -c -o build/reader.o lib/reader.c
|
clang $CFLAGS -c -o build/reader.o lib/reader.c
|
||||||
ar -crs build/lib.a build/memory_pool.o build/reader.o
|
ar -crs build/lib.a build/evaluator.o build/memory_pool.o build/reader.o
|
||||||
|
|
||||||
# Build tests
|
# Build tests
|
||||||
clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c
|
clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c
|
||||||
|
clang $CFLAGS -Itests -c -o build/evaluator_tests.o tests/evaluator_tests.c
|
||||||
|
clang $CFLAGS -o build/evaluator_tests \
|
||||||
|
build/evaluator_tests.o build/lib.a build/testing.o
|
||||||
clang $CFLAGS -Itests -c -o build/reader_tests.o tests/reader_tests.c
|
clang $CFLAGS -Itests -c -o build/reader_tests.o tests/reader_tests.c
|
||||||
clang $CFLAGS -o build/reader_tests \
|
clang $CFLAGS -o build/reader_tests \
|
||||||
build/reader_tests.o build/lib.a build/testing.o
|
build/reader_tests.o build/lib.a build/testing.o
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
cd "$(git rev-parse --show-toplevel)"
|
cd "$(git rev-parse --show-toplevel)"
|
||||||
|
echo ":: Evaluator tests ::" && build/evaluator_tests
|
||||||
echo ":: Reader tests ::" && build/reader_tests
|
echo ":: Reader tests ::" && build/reader_tests
|
||||||
|
33
tests/evaluator_tests.c
Normal file
33
tests/evaluator_tests.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "evaluator.h"
|
||||||
|
#include "memory_pool.h"
|
||||||
|
#include "testing.h"
|
||||||
|
|
||||||
|
static memory_pool_t pool;
|
||||||
|
|
||||||
|
static void number_1234_is_evaluated_as_1234(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
expression_t *expression = allocate_expression(&pool);
|
||||||
|
expression->is_number = true;
|
||||||
|
expression->number = 1234;
|
||||||
|
const int result = evaluate(expression);
|
||||||
|
ASSERT_EQUAL(1234, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void number_4321_is_evaluated_as_4321(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
expression_t *expression = allocate_expression(&pool);
|
||||||
|
expression->is_number = true;
|
||||||
|
expression->number = 4321;
|
||||||
|
const int result = evaluate(expression);
|
||||||
|
ASSERT_EQUAL(4321, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
TESTING_BEGIN();
|
||||||
|
RUN_TEST(number_1234_is_evaluated_as_1234);
|
||||||
|
RUN_TEST(number_4321_is_evaluated_as_4321);
|
||||||
|
TESTING_END();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user