Skip to content

Commit

Permalink
more toml test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
CantelopePeel committed Sep 8, 2023
1 parent 05516ff commit 75cc275
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/util/toml/fd_toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ fd_toml_add_key_value_pair( fd_toml_parser_context_t * ctx, fd_toml_value_t * va
FD_LOG_WARNING(( "ADD KVP2 "));

prev_table_entry->next = new_table_entry;
new_table_entry->key_value_pair.key = ctx->current_key_parts[i];
new_table_entry->key_value_pair.key = ctx->current_key_parts[0];
new_table_entry->key_value_pair.value.value_type = value->value_type;
new_table_entry->key_value_pair.value.value_content = value->value_content;

new_table_entry->next = NULL;
ctx->last_table_entry = new_table_entry;

new_table_entry = new_table_entry->key_value_pair.value.value_content.table_value;
Expand Down Expand Up @@ -524,22 +524,25 @@ fd_toml_parse_table_key_tail( fd_toml_parser_context_t * ctx ) {

int
fd_toml_set_current_table( fd_toml_parser_context_t * ctx ) {
FD_LOG_WARNING(( "SCT1" ));
fd_toml_table_t * prev_table = ctx->root_table_tail;
// FD_TEST( prev_table->next == NULL );

fd_toml_table_t * table = (fd_toml_table_t *)malloc( sizeof( fd_toml_table_t ) );
fd_toml_table_t * table = (fd_toml_table_t *)calloc( sizeof( fd_toml_table_t ), 1 );

for( ulong i = 0; i < ctx->current_key_parts_len; i++ ) {

FD_LOG_WARNING(( "SCT: %s", ctx->current_key_parts[i]));
table->key_value_pair.key = ctx->current_key_parts[i];
table->key_value_pair.value.value_type = fd_toml_value_type_table;
fd_toml_table_t * next_table = (fd_toml_table_t *)malloc( sizeof( fd_toml_table_t ) );
fd_toml_table_t * next_table = (fd_toml_table_t *)calloc( sizeof( fd_toml_table_t ), 1 );
table->key_value_pair.value.value_content.table_value = next_table;
prev_table->next = table;
table->next = NULL;
prev_table = table;
}

ctx->last_table_entry = table;

return FD_TOML_PARSE_SUCCESS;
}

Expand All @@ -565,7 +568,7 @@ fd_toml_parse_table_key( fd_toml_parser_context_t * ctx ) {
return FD_TOML_PARSE_ERR_EXPECTED_KEY;
}

if ( !fd_toml_set_current_table( ctx ) ) {
if ( fd_toml_set_current_table( ctx ) != FD_TOML_PARSE_SUCCESS ) {
return FD_TOML_PARSE_ERR_EXPECTED_KEY;
}

Expand Down Expand Up @@ -666,7 +669,7 @@ fd_toml_to_json_float_value( double float_value, char * buf ) {

char *
fd_toml_to_json_string_value( char * string_value, char * buf ) {
return fd_cstr_append_printf(buf, "{\"type\":\"float\",\"value\":\"%s\"}", string_value);
return fd_cstr_append_printf(buf, "{\"type\":\"string\",\"value\":\"%s\"}", string_value);
}

char *
Expand All @@ -678,6 +681,9 @@ fd_toml_to_json_boolean_value( uint boolean_value, char * buf ) {
}
}

char *
fd_toml_to_json_table( fd_toml_table_t const * table, char * buf );

char *
fd_toml_to_json_value( fd_toml_value_t const * value, char * buf ) {
switch( value->value_type ) {
Expand All @@ -687,6 +693,8 @@ fd_toml_to_json_value( fd_toml_value_t const * value, char * buf ) {
return fd_toml_to_json_integer_value( value->value_content.integer_value, buf );
case fd_toml_value_type_string:
return fd_toml_to_json_string_value( value->value_content.string_value, buf );
case fd_toml_value_type_table:
return fd_toml_to_json_table( value->value_content.table_value, buf );
default:
return fd_cstr_append_printf( buf, "unknown value type" );
}
Expand Down
28 changes: 26 additions & 2 deletions src/util/toml/test_toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,67 @@ FD_IMPORT_CSTR( test_case_3_expected , "src/util/toml/tests/valid/integer/unders
FD_IMPORT_CSTR( test_case_4_input , "src/util/toml/tests/valid/key/alphanum.toml" );
FD_IMPORT_CSTR( test_case_4_expected , "src/util/toml/tests/valid/key/alphanum.json" );

FD_IMPORT_CSTR( test_case_5_input , "src/util/toml/tests/valid/string/simple.toml" );
FD_IMPORT_CSTR( test_case_5_expected , "src/util/toml/tests/valid/string/simple.json" );

FD_IMPORT_CSTR( test_case_6_input , "src/util/toml/tests/valid/string/empty.toml" );
FD_IMPORT_CSTR( test_case_6_expected , "src/util/toml/tests/valid/string/empty.json" );

int
main( int argc,
char ** argv ) {

fd_boot( &argc, &argv );

fd_toml_table_t table_test_case_0;
FD_LOG_NOTICE(( "TEST: %d", fd_toml_parse_document_from_string( test_case_0_input, test_case_0_input_sz, &table_test_case_0 ) ));
// fd_toml_table_t table_test_case_0;
// FD_LOG_NOTICE(( "TEST: %d", fd_toml_parse_document_from_string( test_case_0_input, test_case_0_input_sz, &table_test_case_0 ) ));

fd_toml_table_t table_test_case_1;
FD_LOG_NOTICE(( "TEST: %d", fd_toml_parse_document_from_string( test_case_1_input, test_case_1_input_sz, &table_test_case_1 ) ));

char buf[65536];
memset(buf, 0, sizeof(buf));
fd_toml_to_json( &table_test_case_1, buf );
FD_LOG_NOTICE(( "A: |%s|", buf ));
FD_LOG_NOTICE(( "B: |%s|", test_case_1_expected ));


fd_toml_table_t table_test_case_2;
memset(buf, 0, sizeof(buf));
FD_LOG_NOTICE(( "TEST: %d", fd_toml_parse_document_from_string( test_case_2_input, test_case_2_input_sz, &table_test_case_2 ) ));
fd_toml_to_json( &table_test_case_2, buf );
FD_LOG_NOTICE(( "A: |%s|", buf ));
FD_LOG_NOTICE(( "B: |%s|", test_case_2_expected ));


fd_toml_table_t table_test_case_3;
memset(buf, 0, sizeof(buf));
FD_TEST( fd_toml_parse_document_from_string( test_case_3_input, test_case_3_input_sz, &table_test_case_3 )==0 );
fd_toml_to_json( &table_test_case_3, buf );
FD_LOG_NOTICE(( "A: |%s|", buf ));
FD_LOG_NOTICE(( "B: |%s|", test_case_3_expected ));

fd_toml_table_t table_test_case_4;
memset(buf, 0, sizeof(buf));
FD_TEST( fd_toml_parse_document_from_string( test_case_4_input, test_case_4_input_sz, &table_test_case_4 )==0 );
fd_toml_to_json( &table_test_case_4, buf );
FD_LOG_NOTICE(( "A: |%s|", buf ));
FD_LOG_NOTICE(( "B: |%s|", test_case_4_expected ));

// fd_toml_table_t table_test_case_5;
// memset(buf, 0, sizeof(buf));
// FD_TEST( fd_toml_parse_document_from_string( test_case_5_input, test_case_5_input_sz, &table_test_case_5 )==0 );
// fd_toml_to_json( &table_test_case_5, buf );
// FD_LOG_NOTICE(( "A: |%s|", buf ));
// FD_LOG_NOTICE(( "B: |%s|", test_case_5_expected ));

// fd_toml_table_t table_test_case_6;
// memset(buf, 0, sizeof(buf));
// FD_TEST( fd_toml_parse_document_from_string( test_case_6_input, test_case_6_input_sz, &table_test_case_6 )==0 );
// fd_toml_to_json( &table_test_case_6, buf );
// FD_LOG_NOTICE(( "A: |%s|", buf ));
// FD_LOG_NOTICE(( "B: |%s|", test_case_6_expected ));

FD_LOG_NOTICE(( "pass" ));
fd_halt();
return 0;
Expand Down
10 changes: 10 additions & 0 deletions src/util/toml/tests/valid/integer/long.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"int64-max": {
"type": "integer",
"value": "9223372036854775807"
},
"int64-max-neg": {
"type": "integer",
"value": "-9223372036854775808"
}
}
2 changes: 2 additions & 0 deletions src/util/toml/tests/valid/integer/long.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int64-max = 9223372036854775807
int64-max-neg = -9223372036854775808
6 changes: 6 additions & 0 deletions src/util/toml/tests/valid/string/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"answer": {
"type": "string",
"value": ""
}
}
1 change: 1 addition & 0 deletions src/util/toml/tests/valid/string/empty.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
answer = ""
6 changes: 6 additions & 0 deletions src/util/toml/tests/valid/string/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"answer": {
"type": "string",
"value": "You are not drinking enough whisky."
}
}
1 change: 1 addition & 0 deletions src/util/toml/tests/valid/string/simple.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
answer = "You are not drinking enough whisky."

0 comments on commit 75cc275

Please sign in to comment.