]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Added some test for corner cases to improve code coverage and stability
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 28 Oct 2014 16:19:48 +0000 (12:19 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 28 Oct 2014 16:19:48 +0000 (12:19 -0400)
source/sclpl/lexer.c
source/sclpl/scanner.c
spec/lexer_spec.rb

index c95c5f81dd2469ef1b76324fa1f32b60ef074c9d..9078357a04f3d40490aa9cb9f3ce988a69cff98d 100644 (file)
@@ -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;
 }
index 17e6b125dde41423b0e09fff00f9469226da7eb3..e6360879c77ac5aa9b5cf3fa412b455d5d5c05bb 100644 (file)
@@ -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))
index 51a81b12293570ab5b068cb72a4277dfc97dd58d..cdf6b312190adae2b0732ffcf98ac6d93d935440 100644 (file)
@@ -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