Implement decimal integer reading
This commit is contained in:
parent
077245b3c7
commit
f251fd04cc
9
lib/include/reader.h
Normal file
9
lib/include/reader.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef READER_H
|
||||||
|
#define READER_H
|
||||||
|
|
||||||
|
#include "memory_pool.h"
|
||||||
|
|
||||||
|
const expression_t *
|
||||||
|
read_expression(memory_pool_t *pool, const char *input, int len);
|
||||||
|
|
||||||
|
#endif
|
21
lib/reader.c
Normal file
21
lib/reader.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "reader.h"
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
const expression_t *
|
||||||
|
read_expression(memory_pool_t *pool, const char *input, int len)
|
||||||
|
{
|
||||||
|
expression_t *result = allocate_expression(pool);
|
||||||
|
if (NULL == result)
|
||||||
|
return NULL;
|
||||||
|
result->is_number = true;
|
||||||
|
result->number = 0;
|
||||||
|
while (isdigit(*input)) {
|
||||||
|
result->number *= 10;
|
||||||
|
result->number += *input - '0';
|
||||||
|
++input;
|
||||||
|
++len;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
@ -13,6 +13,9 @@ ar -crs build/lib.a 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/reader_tests.o tests/reader_tests.c
|
||||||
|
clang $CFLAGS -o build/reader_tests \
|
||||||
|
build/reader_tests.o build/lib.a build/testing.o
|
||||||
|
|
||||||
# Build application
|
# Build application
|
||||||
clang $CFLAGS -c -o build/main.o app/main.c
|
clang $CFLAGS -c -o build/main.o app/main.c
|
||||||
|
30
tests/reader_tests.c
Normal file
30
tests/reader_tests.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "reader.h"
|
||||||
|
#include "testing.h"
|
||||||
|
|
||||||
|
static memory_pool_t pool;
|
||||||
|
|
||||||
|
static void input_1234_is_read_as_number_with_expected_value(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
const expression_t *result = read_expression(&pool, "1234", 4);
|
||||||
|
ASSERT_NOT_NULL(result);
|
||||||
|
ASSERT_TRUE(result->is_number);
|
||||||
|
ASSERT_EQUAL(1234, result->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void input_4321_is_read_as_number_with_expected_value(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
const expression_t *result = read_expression(&pool, "4321", 4);
|
||||||
|
ASSERT_NOT_NULL(result);
|
||||||
|
ASSERT_TRUE(result->is_number);
|
||||||
|
ASSERT_EQUAL(4321, result->number);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
TESTING_BEGIN();
|
||||||
|
RUN_TEST(input_1234_is_read_as_number_with_expected_value);
|
||||||
|
RUN_TEST(input_4321_is_read_as_number_with_expected_value);
|
||||||
|
TESTING_END();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user