From 8c1a4be939e480fa3aa5939fd10aa3af86387d15 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 28 Oct 2014 12:19:48 -0400 Subject: [PATCH] Added some test for corner cases to improve code coverage and stability --- source/sclpl/lexer.c | 20 +++++++++++++------- source/sclpl/scanner.c | 6 ++++-- spec/lexer_spec.rb | 27 ++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/source/sclpl/lexer.c b/source/sclpl/lexer.c index c95c5f8..9078357 100644 --- a/source/sclpl/lexer.c +++ b/source/sclpl/lexer.c @@ -62,7 +62,7 @@ void lexer_skipline(lexer_t* p_lexer) { static lex_tok_t* lexer_make_token(char* text) { lex_tok_t* p_tok = NULL; - if ((0 == strcmp(text,"end") || (text[0] == ';'))) { + if (0 == strcmp(text,"end")) { p_tok = lex_tok_new(T_END, NULL); } else if (lexer_oneof("()[]{};,'", text[0])) { p_tok = lexer_punc(text); @@ -119,13 +119,18 @@ static lex_tok_t* lexer_char(char* text) break; } } + if (NULL == p_tok) + p_tok = lexer_var(text); } return p_tok; } static lex_tok_t* lexer_radix_int(char* text) { - return lexer_integer(&text[2], read_radix(text[1])); + lex_tok_t* ret = lexer_integer(&text[2], read_radix(text[1])); + if (NULL == ret) + ret = lexer_var(text); + return ret; } static lex_tok_t* lexer_number(char* text) @@ -198,11 +203,12 @@ char* lexer_dup(const char* p_old) { } static int read_radix(char ch) { + int ret = -1; switch(ch) { - case 'b': return 2; - case 'o': return 8; - case 'd': return 10; - case 'h': return 16; - default: return 10; + case 'b': ret = 2; break; + case 'o': ret = 8; break; + case 'd': ret = 10; break; + case 'h': ret = 16; break; } + return ret; } diff --git a/source/sclpl/scanner.c b/source/sclpl/scanner.c index 17e6b12..e636087 100644 --- a/source/sclpl/scanner.c +++ b/source/sclpl/scanner.c @@ -46,7 +46,7 @@ char* scanner_read(scanner_t* p_scanner) { static char* scanner_read_string(scanner_t* p_scanner) { size_t capacity = 8; size_t index = 0; - char* tok = (char*)malloc(sizeof(capacity)); + char* tok = (char*)malloc(capacity); /* Skip the first " */ tok[index++] = scanner_current(p_scanner); @@ -56,8 +56,10 @@ static char* scanner_read_string(scanner_t* p_scanner) { /* Read the contents of the string */ while ('"' != scanner_current(p_scanner)) { /* Resize the buffer if necessary */ - if ((index+2) >= capacity) + if ((index+2) >= capacity) { capacity = capacity << 1; + tok = (char*)realloc(tok, capacity); + } /* EOF results in an assertion (don't do) */ if (scanner_eof(p_scanner)) diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb index 51a81b1..cdf6b31 100644 --- a/spec/lexer_spec.rb +++ b/spec/lexer_spec.rb @@ -69,6 +69,10 @@ describe "lexer" do it "should recognize 'c'" do expect(lexer('\c')).to eq ['T_CHAR:\c'] end + + it "should recognize invalid named characters as identifiers" do + expect(lexer('\foobar')).to eq ['T_VAR:\foobar'] + end end context "numbers" do @@ -84,6 +88,10 @@ describe "lexer" do it "should recognize negitve integer with sign" do expect(lexer('-123')).to eq ['T_INT:-123'] end + + it "should recognize invalid ints as identifiers" do + expect(lexer('123a')).to eq ['T_VAR:123a'] + end end context "radix integers" do @@ -102,6 +110,10 @@ describe "lexer" do it "should recognize decimal integer" do expect(lexer('0hf0f')).to eq ['T_INT:3855'] end + + it "should recognize invalid radix ints as identifiers" do + expect(lexer('0b012')).to eq ['T_VAR:0b012'] + end end context "floating point" do @@ -113,9 +125,13 @@ describe "lexer" do expect(lexer('+123.0')).to eq ['T_FLOAT:123.000000'] end - it "should recognize negitve float with sign" do + it "should recognize negative float with sign" do expect(lexer('-123.0')).to eq ['T_FLOAT:-123.000000'] end + + it "should recognize invalid floats as identifiers" do + expect(lexer('123..0')).to eq ['T_VAR:123..0'] + end end end @@ -155,6 +171,15 @@ describe "lexer" do it "should recognize a string that spans lines" do expect(lexer("\"a\nb\"")).to eq ["T_STRING:\"a\nb\""] end + + it "should recognize larger strings" do + expect(lexer("\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"")).to eq [ + "T_STRING:\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\""] + end + + it "should raise an assertion exception when the file ends before a string terminates" do + expect{lexer("\"abc")}.to raise_error(/AssertionException/) + end end end -- 2.52.0