/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
1 by Gustav Hartvigsson
* Initial code - far from done
1
using VQDR.Expression;
2
3
void
4
test_value_token () {
5
  GLib.Test.add_func ("/VQDR/Exprossion/Token/Value/sanity", () => {
6
    var ctx = new Context ();
7
    
8
    if (ctx == null) {
9
      GLib.Test.message ("Context was null.");
10
      GLib.Test.fail ();
11
      GLib.assert_not_reached ();
12
    }
13
    
14
    if (!(ctx.get_type ().is_a (typeof (Context)))) {
15
      GLib.Test.message ("Context is not the right type.");
16
      GLib.Test.fail ();
17
      GLib.assert_not_reached ();
18
    }
19
    
20
    var v1 = new ConstantValueToken (13,37);
21
    var v2 = new VariableValueToken ("my-val", 13);
22
    
23
    #if 0
24
    // This should work, but it does not... It is a vala parsing bug...
25
    // I think...
26
    bool value_test = ((v1 is typeof (ConstantValueToken)) ||
27
                       (v1 is typeof (ValueToken)) ||
28
                       (v1 is typeof (Token)));
29
    
30
    #endif
31
    
32
    Type t1 = v1.get_type ();
33
    if (!(t1.is_a (typeof (ConstantValueToken))) ||
34
        !(t1.is_a (typeof (ValueToken))) ||
35
        !(t1.is_a (typeof (Token)))) {
36
      GLib.Test.message ("The ConstastValueToken is not the corret type.");
37
      GLib.Test.fail ();
38
      GLib.assert_not_reached ();
39
    }
40
    
41
   Type t2 = v2.get_type ();
42
    if (!(t2.is_a (typeof (VariableValueToken))) ||
43
        !(t2.is_a (typeof (ValueToken))) ||
44
        !(t2.is_a (typeof (Token)))) {
45
      GLib.Test.message ("The VariableValueToken is not the corret type.");
46
      GLib.Test.fail ();
47
      GLib.assert_not_reached ();
48
    }
49
    
50
    
51
  });
52
  
53
  GLib.Test.add_func ("/VQDR/Exprossion/Token/Value/Constant", () => {
54
    try {
55
      long in_val = 12;
56
      
57
      var ctx = new Context ();
58
      
59
      var v1 = new ConstantValueToken (in_val, 1);
60
      
61
      var root_t = new RootToken (v1);
62
      
63
      root_t.evaluate (ctx);
64
      
65
      long out_val = root_t.result_value;
66
      
67
      if (out_val != in_val) {
68
        GLib.Test.message ("The values do not match");
69
        GLib.Test.fail ();
70
      }
71
    } catch (GLib.Error? e) {
72
       GLib.Test.message ("An error occured: domain: %s, message: %s", e.domain.to_string (), e.message);
73
       GLib.Test.fail ();
74
    }
75
  });
76
  
77
  GLib.Test.add_func ("/VQDR/Exprossion/Token/Value/ConstantLoop", () => {
78
    
79
    
80
    try {
81
      var ctx = new Context ();
82
      
83
      for (int i = 0; i <= 10000; i++ ) {
84
        var v1 = new ConstantValueToken (i, 1);
85
        
86
        var root_t = new RootToken (v1);
87
        
88
        root_t.evaluate (ctx);
89
        
90
        long out_val = root_t.result_value;
91
        
92
        if (out_val != i) {
93
          GLib.Test.message ("The values do not match");
94
          GLib.Test.fail ();
95
        }
96
      }
97
    } catch (GLib.Error? e) {
98
       GLib.Test.message ("An error occured: domain: %s, message: %s", e.domain.to_string (), e.message);
99
       GLib.Test.fail ();
100
    }
101
  });
102
  
103
  GLib.Test.add_func ("/VQDR/Exprossion/Token/Value/Varuable", () => {
104
    try {
105
      var ctx = new Context ();
106
      ctx.set_value ("a", 0, 0, 13);
107
      ctx.set_value ("not a", 0, 0, 37);
108
      
109
      // Context variable santy check.
110
      if ((ctx.get_value ("a") != 13) || (ctx.get_value ("not a") != 37)) {
111
          GLib.Test.message ("The values do not match");
112
          
113
          GLib.Test.fail ();
114
      }
115
      
116
      var v1 = new VariableValueToken ("a", 1);
117
      var v2 = new VariableValueToken ("not a", 1);
118
      
119
    } catch (GLib.Error? e) {
120
       GLib.Test.message ("An error occured: domain: %s, message: %s", e.domain.to_string (), e.message);
121
       GLib.Test.fail ();
122
    }
123
  });
124
  
125
  
126
}