Refactor unit tests some.
2 files changed, 111 insertions(+), 101 deletions(-)

M src/compile/mod.rs
M src/compile/tests.rs
M src/compile/mod.rs +21 -2
@@ 49,12 49,31 @@ impl CContext {
         let consts = FnvHashMap::default();
         // Global symbol table for functions, consts, etc...
         let symtable = vec![FnvHashMap::default()];
-        Self {
+        let mut s = Self {
             b,
             typetable,
             consts,
             symtable,
-        }
+        };
+        // Add default types so they're always in a known order, which makes
+        // unit testing easier.
+        use verify::TypeDef;
+        s.add_type(&TypeDef::F32);
+        // vec4<f32>
+        s.add_type(&TypeDef::Struct(vec![
+            ("x".into(), TypeDef::F32),
+            ("y".into(), TypeDef::F32),
+            ("z".into(), TypeDef::F32),
+            ("w".into(), TypeDef::F32),
+        ]));
+        // f32 -> f32
+        s.add_type(&TypeDef::Function(
+            vec![TypeDef::F32],
+            Box::new(TypeDef::F32),
+        ));
+        s.add_type(&TypeDef::Bool);
+        s.add_type(&TypeDef::Unit);
+        s
     }
 
     /// Pushes a new scope to the symbol table.

          
M src/compile/tests.rs +90 -99
@@ 30,6 30,12 @@ fn validate_module_header(asm: &str) -> 
     while p.peek().unwrap().contains("OpName ") {
         p.next();
     }
+    // Skip built-in types
+    assert!(p.next().unwrap().contains("%1 = OpTypeFloat 32"));
+    assert!(p.next().unwrap().contains("%2 = OpTypeStruct %1 %1 %1 %1"));
+    assert!(p.next().unwrap().contains("%3 = OpTypeFunction %1 %1"));
+    assert!(p.next().unwrap().contains("%4 = OpTypeBool"));
+    assert!(p.next().unwrap().contains("%5 = OpTypeVoid"));
     p
 }
 

          
@@ 122,29 128,25 @@ fn test_compile_function() {
     println!("{}", dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %1
-%4 = OpTypeFunction %2 %2
-%5 = OpTypeBool
-%6 = OpTypeVoid
-%9 = OpConstant  %1  1.0
-%13 = OpTypeFunction %6
-%7 = OpFunction  %1  DontInline|Pure|Const %3
-%8 = OpLabel
-OpReturnValue %9
+        r#"%6 = OpTypeFunction %1                     
+%7 = OpTypeFunction %2 %2                   
+%10 = OpConstant  %1  1.0                                                                      
+%14 = OpTypeFunction %5                                                  
+%8 = OpFunction  %1  DontInline|Pure|Const %6                                            
+%9 = OpLabel                               
+OpReturnValue %10                            
+OpFunctionEnd                                                                  
+%11 = OpFunction  %2  DontInline|Pure|Const %7                                             
+%12 = OpFunctionParameter  %2                                                                  
+%13 = OpLabel                                                                                                      
+OpReturnValue %12
 OpFunctionEnd
-%10 = OpFunction  %2  DontInline|Pure|Const %4
-%11 = OpFunctionParameter  %2 
-%12 = OpLabel
-OpReturnValue %11
-OpFunctionEnd
-%14 = OpFunction  %6  DontInline|Const %13
-%15 = OpLabel
+%15 = OpFunction  %5  DontInline|Const %14
+%16 = OpLabel
 OpReturn
 OpFunctionEnd
-%16 = OpFunction  %6  DontInline|Const %13
-%17 = OpLabel
+%17 = OpFunction  %5  DontInline|Const %14
+%18 = OpLabel
 OpReturn
 OpFunctionEnd"#,
     );

          
@@ 181,28 183,23 @@ fn test_compile_identity() {
     let rest = validate_module_header(&dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %2 %2
-%4 = OpTypeFunction %1 %1
-%5 = OpTypeBool
-%6 = OpTypeVoid
-%13 = OpTypeFunction %6
-%7 = OpFunction  %1  DontInline|Pure|Const %4
-%8 = OpFunctionParameter  %1
+        r#"%6 = OpTypeFunction %2 %2
+%13 = OpTypeFunction %5                                                                                           
+%7 = OpFunction  %1  DontInline|Pure|Const %3                                                                     
+%8 = OpFunctionParameter  %1                                                                                      
 %9 = OpLabel
 OpReturnValue %8
 OpFunctionEnd
-%10 = OpFunction  %2  DontInline|Pure|Const %3
-%11 = OpFunctionParameter  %2
+%10 = OpFunction  %2  DontInline|Pure|Const %6                                                                    
+%11 = OpFunctionParameter  %2                                                                                     
 %12 = OpLabel
 OpReturnValue %11
 OpFunctionEnd
-%14 = OpFunction  %6  DontInline|Const %13
+%14 = OpFunction  %5  DontInline|Const %13
 %15 = OpLabel
 OpReturn
 OpFunctionEnd
-%16 = OpFunction  %6  DontInline|Const %13
+%16 = OpFunction  %5  DontInline|Const %13
 %17 = OpLabel
 OpReturn
 OpFunctionEnd"#,

          
@@ 243,32 240,28 @@ fn test_compile_literals() {
     let rest = validate_module_header(&dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %1
-%4 = OpTypeFunction %2 %2                                                                                         
-%5 = OpTypeBool
-%6 = OpTypeVoid
-%9 = OpConstantTrue  %5                                                                                           
-%10 = OpConstantFalse  %5                                                                                         
-%11 = OpConstant  %1  -99.1
-%12 = OpConstant  %1  3.0                                                                                         
-%16 = OpTypeFunction %6                                                                                           
-%7 = OpFunction  %1  DontInline|Pure|Const %3                                                                     
-%8 = OpLabel
-OpReturnValue %12
+        r#"%6 = OpTypeFunction %1                                                                                            
+%7 = OpTypeFunction %2 %2
+%10 = OpConstantTrue  %4                                                                                          
+%11 = OpConstantFalse  %4
+%12 = OpConstant  %1  -99.1
+%13 = OpConstant  %1  3.0                                                                                         
+%17 = OpTypeFunction %5                                                                                           
+%8 = OpFunction  %1  DontInline|Pure|Const %6                                                                     
+%9 = OpLabel
+OpReturnValue %13
 OpFunctionEnd
-%13 = OpFunction  %2  DontInline|Pure|Const %4                                                                    
-%14 = OpFunctionParameter  %2                                                                                     
-%15 = OpLabel
-OpReturnValue %14
+%14 = OpFunction  %2  DontInline|Pure|Const %7                                                                    
+%15 = OpFunctionParameter  %2                                                                                     
+%16 = OpLabel
+OpReturnValue %15
 OpFunctionEnd
-%17 = OpFunction  %6  DontInline|Const %16
-%18 = OpLabel
+%18 = OpFunction  %5  DontInline|Const %17
+%19 = OpLabel
 OpReturn
-OpFunctionEnd                                                                                                     
-%19 = OpFunction  %6  DontInline|Const %16
-%20 = OpLabel
+OpFunctionEnd
+%20 = OpFunction  %5  DontInline|Const %17
+%21 = OpLabel                                                                                                     
 OpReturn
 OpFunctionEnd"#
     );

          
@@ 308,12 301,7 @@ fn test_compile_funcall() {
     let rest = validate_module_header(&dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %1 %1
-%4 = OpTypeBool                                                                                                   
-%5 = OpTypeVoid
-%13 = OpTypeFunction %5
+        r#"%13 = OpTypeFunction %5
 %6 = OpFunction  %1  DontInline|Pure|Const %3                                                                     
 %7 = OpFunctionParameter  %1                                                                                      
 %8 = OpLabel                                                                                                      

          
@@ 370,12 358,7 @@ fn test_compile_recursive_funcall() {
     let rest = validate_module_header(&dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %1 %1
-%4 = OpTypeBool                                                                                                   
-%5 = OpTypeVoid
-%13 = OpTypeFunction %5
+        r#"%13 = OpTypeFunction %5
 %6 = OpFunction  %1  DontInline|Pure|Const %3                                                                     
 %7 = OpFunctionParameter  %1                                                                                      
 %8 = OpLabel                                                                                                      

          
@@ 423,11 406,16 @@ fn test_compile_types_misc() {
     let _ = c.get_type(&verify::TypeDef::Unit);
 
     let dis = c.b.module().disassemble();
+    println!("{}", dis);
     let rest = validate_header(&dis);
+    // We include a pile of default types: f32, vec4<f32>, f32->f32, bool, void.
     validate_lines(
         rest,
-        r#"%1 = OpTypeBool
-%2 = OpTypeVoid"#,
+        r#"%1 = OpTypeFloat 32
+%2 = OpTypeStruct %1 %1 %1 %1
+%3 = OpTypeFunction %1 %1                                                                                           
+%4 = OpTypeBool                                                                                                     
+%5 = OpTypeVoid"#
     );
 }
 

          
@@ 449,14 437,17 @@ fn test_compile_types_struct() {
     let _t1 = c.add_type(&def2);
 
     let dis = c.b.module().disassemble();
-    // println!("{}", dis);
+    //println!("{}", dis);
     let rest = validate_header(&dis);
     validate_lines(
         rest,
         r#"%1 = OpTypeFloat 32
-%2 = OpTypeBool
-%3 = OpTypeStruct %1 %1 %2
-%4 = OpTypeStruct %3 %1 %3"#,
+%2 = OpTypeStruct %1 %1 %1 %1
+%3 = OpTypeFunction %1 %1
+%4 = OpTypeBool
+%5 = OpTypeVoid
+%6 = OpTypeStruct %1 %1 %4
+%7 = OpTypeStruct %6 %1 %6"#,
     );
 }
 

          
@@ 484,9 475,12 @@ fn test_compile_structdefs() {
     validate_lines(
         rest,
         r#"%1 = OpTypeFloat 32
-%2 = OpTypeBool
-%3 = OpTypeStruct %1 %1 %2
-%4 = OpTypeStruct %3 %1 %3"#,
+%2 = OpTypeStruct %1 %1 %1 %1
+%3 = OpTypeFunction %1 %1                                                                                           
+%4 = OpTypeBool
+%5 = OpTypeVoid
+%6 = OpTypeStruct %1 %1 %4                                                                                          
+%7 = OpTypeStruct %6 %1 %6"#,
     );
 }
 

          
@@ 508,9 502,10 @@ fn test_compile_add() {
             typ: ast::Type("F32".into()),
         }],
         returns: ast::Type("F32".into()),
-        body: vec![ast::Expr::FunCall(
-            "vertex".into(),
-            vec![Expr::Var("input".into())],
+        body: vec![Expr::BinOp(
+            Op::Add,
+            Box::new(Expr::Literal(Lit::F32(5.0))),
+            Box::new(Expr::Var("input".into())),
         )],
     });
     let program = vec![vert, frag];

          
@@ 523,30 518,26 @@ fn test_compile_add() {
     let rest = validate_module_header(&dis);
     validate_lines(
         rest,
-        r#"%1 = OpTypeFloat 32
-%2 = OpTypeStruct %1 %1 %1 %1
-%3 = OpTypeFunction %1 %1
-%4 = OpTypeBool                                                                                                   
-%5 = OpTypeVoid
-%13 = OpTypeFunction %5
-%6 = OpFunction  %1  DontInline|Pure|Const %3                                                                     
-%7 = OpFunctionParameter  %1                                                                                      
-%8 = OpLabel                                                                                                      
+        r#"%12 = OpConstant  %1  5.0
+%14 = OpTypeFunction %5
+%6 = OpFunction  %1  DontInline|Pure|Const %3
+%7 = OpFunctionParameter  %1
+%8 = OpLabel
 OpReturnValue %7
 OpFunctionEnd
 %9 = OpFunction  %1  DontInline|Pure|Const %3
-%10 = OpFunctionParameter  %1                                                                                     
-%11 = OpLabel                                                                                                     
-%12 = OpFunctionCall  %1  %6 %10
-OpReturnValue %12
+%10 = OpFunctionParameter  %1
+%11 = OpLabel
+%13 = OpFAdd  %1  %12 %10
+OpReturnValue %13
 OpFunctionEnd
-%14 = OpFunction  %5  DontInline|Const %13
-%15 = OpLabel
+%15 = OpFunction  %5  DontInline|Const %14
+%16 = OpLabel
 OpReturn
-OpFunctionEnd                                                                                                     
-%16 = OpFunction  %5  DontInline|Const %13
-%17 = OpLabel
-OpReturn                                                                                                          
+OpFunctionEnd
+%17 = OpFunction  %5  DontInline|Const %14
+%18 = OpLabel
+OpReturn
 OpFunctionEnd"#,
     );
     spirv_val(&m);