tests.py 54.1 KB
Newer Older
Sergio Costas's avatar
Sergio Costas committed
1 2 3 4
#!/usr/bin/env python3

import sys
import os
5
#sys.path.append(os.path.abspath(os.path.join(os.getcwd(), os.pardir)))
Sergio Costas's avatar
Sergio Costas committed
6 7

import unittest
8
from crust import crust
Sergio Costas's avatar
Sergio Costas committed
9 10 11

class Test(unittest.TestCase):

Sergio Costas's avatar
Sergio Costas committed
12
    do_print_debug = False
13
    do_print_debug2 = False
14

15
    def _find_variable(self, var_container, var_name):
16
        for var_group in var_container["variables"]:
17 18 19 20
            if var_name in var_group:
                return var_group[var_name]
        return None

21 22 23 24 25 26 27
    def _find_variable_by_id(self, var_container, var_id):
        for var_group in var_container["variables"]:
            for var_name in var_group:
                if var_group[var_name]["uid"] == var_id:
                    return var_group[var_name]
        return None

28
    def _check_var_state(self, lib, variable, status, deep = 0):
29 30
        var = self._find_variable(lib.debug[deep], variable)
        self.assertIsNot(var, None)
31
        self.assertEqual(var["value"], status)
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46
    def _check_block_id_is_the_same(self, lib, variable1, variable2, deep = 0):
        var1 = self._find_variable(lib.debug[deep], variable1)
        self.assertIsNot(var1, None)
        var2 = self._find_variable(lib.debug[deep], variable2)
        self.assertIsNot(var2, None)
        self.assertEqual(var1["uid"], var2["uid"])

    def _check_block_id_is_not_same(self, lib, variable1, variable2, deep = 0):
        var1 = self._find_variable(lib.debug[deep], variable1)
        self.assertIsNot(var1, None)
        var2 = self._find_variable(lib.debug[deep], variable2)
        self.assertIsNot(var2, None)
        self.assertNotEqual(var1["uid"], var2["uid"])

47
    def _test_msg(self, lib, error_element, params):
48 49 50
        with open("error_list.txt", "a") as errorlist:
            errorlist.write(error_element[1])
            errorlist.write("\n")
51
        self.assertEqual(len(error_element), len(params))
Sergio Costas's avatar
Sergio Costas committed
52
        for index in range(len(error_element)):
53
            self.assertEqual(error_element[index], params[index])
Sergio Costas's avatar
Sergio Costas committed
54

55
    def _print_errors(self, filename, lib):
56
        if self.do_print_debug2:
57
            print("\rtest file: {:s}".format(filename))
58
            lib.print_errors(False)
59

60
    def open_crust(self):
61 62
        if (os.path.exists('lexyacclib/crust_code.h')) and (os.path.exists('lexyacclib/crust.so')):
            lib = crust.crust("lexyacclib/crust_code.h", "lexyacclib/crust.so")
63
        else:
64
            lib = crust.crust(pkg_resources.resource_filename('crust','/crust_code.h'),pkg_resources.resource_filename('crust','/crust.so'))
65 66
        return lib

67
    def _generic_test(self, filename, params):
68 69
        if self.do_print_debug:
            print("Running test {:s}".format(filename))
70
        lib = self.open_crust()
71
        tree = lib.read_file(filename, False, [], [], [])
72
        self.assertIsNot(tree, None)
73
        variables = lib.process_tree(tree)
74 75 76
        lib.process_functions(tree, variables)
        self._print_errors(filename, lib)
        self.assertEqual(len(lib.errors), len(params))
77 78 79
        # avoid problems with error order
        lib.errors.sort()
        params.sort()
80
        for i in range(len(lib.errors)):
81
            self._test_msg(lib, lib.errors[i], params[i])
82
        return lib
Sergio Costas's avatar
Sergio Costas committed
83 84


85
    def _all_fine_test(self, filename):
86 87
        if self.do_print_debug:
            print("Running test {:s}".format(filename))
88
        lib = self.open_crust()
89
        tree = lib.read_file(filename, False, [], [], [])
90
        self.assertIsNot(tree, None)
91
        variables = lib.process_tree(tree)
92 93 94
        lib.process_functions(tree, variables)
        self._print_errors(filename, lib)
        self.assertEqual(len(lib.errors), 0)
95
        return lib
Sergio Costas's avatar
Sergio Costas committed
96

97
    def _raise_test(self, filename, exception_class, params):
98 99
        if self.do_print_debug:
            print("Running test {:s}".format(filename))
100
        lib = self.open_crust()
101
        tree = lib.read_file(filename, False, [], [], [])
102
        self.assertIsNot(tree, None)
103
        variables = lib.process_tree(tree)
104
        with self.assertRaises(exception_class):
105 106 107
            lib.process_functions(tree, variables)
        self._print_errors(filename, lib)
        self.assertEqual(len(lib.errors), len(params))
108
        for i in range(len(lib.errors)):
109
            self._test_msg(lib, lib.errors[i], params[i])
110 111
        return lib

112
    def test001AllFine(self):
113 114 115
        # This test just must work
        self._all_fine_test("unitest/test1.c")

116
    def test002UnknownVariable(self):
117
        self._raise_test("unitest/test2.c", crust.crust.VarNotFoundException, [(crust.crust.MSG_CRITICAL, "Unknown variable '{:s}' at line {:d}", "var", 4)])
Sergio Costas's avatar
Sergio Costas committed
118

119
    def test003Overwrite(self):
120
        self._generic_test("unitest/test3.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param3", 7, 6) ])
Sergio Costas's avatar
Sergio Costas committed
121

122
    def test004UseUninitialized(self):
123
        self._generic_test("unitest/test4.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} isn't initialized", 1, "function", 7) ])
Sergio Costas's avatar
Sergio Costas committed
124

125
    def test005UseFreed(self):
126
        self._generic_test("unitest/test5.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 6, 5) ])
Sergio Costas's avatar
Sergio Costas committed
127

128
    def test006PassedNonCrustyToCrusty(self):
129
        self._generic_test("unitest/test6.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable", 1, "function", 8) ])
Sergio Costas's avatar
Sergio Costas committed
130

131
    def test007PassedCrustyToNonCrusty(self):
132
        self._generic_test("unitest/test7.c", [ (crust.crust.MSG_CRITICAL, "Expected a non __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable", 1, "function", 8) ])
Sergio Costas's avatar
Sergio Costas committed
133

134
    def test008PassedOtherToCrusty(self):
135
        self._generic_test("unitest/test8.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}", 1, "function", 7) ])
Sergio Costas's avatar
Sergio Costas committed
136

137
    def test009PassedIncorrectNumberOfParameters(self):
138
        self._generic_test("unitest/test9.c", [ (crust.crust.MSG_CRITICAL, "Calling function '{:s}' at line {:d} with an incorrect number of arguments", "function", 7) ])
Sergio Costas's avatar
Sergio Costas committed
139

140
    def test010NonExistingFunction(self):
141
        self._raise_test("unitest/test10.c", crust.crust.FunctionNotFoundException, [ (crust.crust.MSG_CRITICAL, "Calling function '{:s}' at line {:d}, but it is not declared or defined", "function3", 5) ])
Sergio Costas's avatar
Sergio Costas committed
142

143
    def test011CrustParameterNotPointer(self):
144
        self._generic_test("unitest/test11.c", [ (crust.crust.MSG_CRITICAL, "An argument is defined as __crust_t__ type at line {:d}, but it is not a pointer", 1) ])
Sergio Costas's avatar
Sergio Costas committed
145

146
    def test012CrustNamedParameterNotPointer(self):
147
        self._generic_test("unitest/test12.c", [ (crust.crust.MSG_CRITICAL, "Argument '{:s}' defined as __crust_t__ type at line {:d}, but it is not a pointer", "parameter", 1) ])
Sergio Costas's avatar
Sergio Costas committed
148

149
    def test013CrustVariableNotPointer(self):
150
        self._generic_test("unitest/test13.c", [ (crust.crust.MSG_CRITICAL, "Variable '{:s}' is defined as __crust_t__ type at line {:d}, but it is not a pointer", "retval", 5) ])
151

152
    def test014CrustRetvalNotPointer(self):
153
        self._generic_test("unitest/test14.c", [ (crust.crust.MSG_CRITICAL, "Return value for function '{:s}', defined at line {:d}, is defined as __crust_t__ type, but it is not a pointer", "function", 1) ])
Sergio Costas's avatar
Sergio Costas committed
154

155
    def test015CrustOnlyVarNoOthers(self):
156
        self._generic_test("unitest/test15.c", [ (crust.crust.MSG_CRITICAL, "Variable '{:s}', defined at line {:d}, have the __crust_recycle__ modifier, which is not allowed for defined variables", "var", 1) ])
Sergio Costas's avatar
Sergio Costas committed
157

158
    def test016RecycleOrBorrowNotBoth(self):
159
        self._generic_test("unitest/test16.c", [ (crust.crust.MSG_CRITICAL, "An argument can be BORROW or RECYCLE, not both. (error at line {:d})", 1) ])
Sergio Costas's avatar
Sergio Costas committed
160

161
    def test017NamedRecycleOrBorrowNotBoth(self):
162
        self._generic_test("unitest/test17.c", [ (crust.crust.MSG_CRITICAL, "An argument can be BORROW or RECYCLE, not both. (argument '{:s}' at line {:d})", "param", 1) ])
Sergio Costas's avatar
Sergio Costas committed
163

164
    def test018RecycleReturnsCrust(self):
165
        self._generic_test("unitest/test18.c", [ (crust.crust.MSG_CRITICAL, "The function '{:s}', at line {:d}, has a RECYCLE argument, but doesn't return a CRUST pointer", "function", 1) ])
Sergio Costas's avatar
Sergio Costas committed
166

167
    def test019OnlyOneRecycle(self):
168
        self._generic_test("unitest/test19.c", [ (crust.crust.MSG_CRITICAL, "The function '{:s}', at line {:d}, has more than one RECYCLE argument", "function", 1) ])
Sergio Costas's avatar
Sergio Costas committed
169

170
    def test020UninitializedVariable(self):
171
        self._generic_test("unitest/test20.c", [ (crust.crust.MSG_ERROR, "Using uninitialized variable '{:s}' at line {:d}", "param4", 5) ])
Sergio Costas's avatar
Sergio Costas committed
172

173
    def test021FreedVariable(self):
174
        self._generic_test("unitest/test21.c", [ (crust.crust.MSG_ERROR, "Using variable '{:s}' at line {:d}, after being freed at line {:d}", "param4", 8, 7) ])
Sergio Costas's avatar
Sergio Costas committed
175

176
    def test022AssignFreedVariable(self):
177
        self._generic_test("unitest/test22.c", [ (crust.crust.MSG_ERROR, "Using variable '{:s}' at line {:d}, after being freed at line {:d}", "param1", 5, 4), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param4", 4, 7) ])
178

179
    def test023AssignUninitializedVariable(self):
180
        self._generic_test("unitest/test23.c", [ (crust.crust.MSG_ERROR, "Using uninitialized variable '{:s}' at line {:d}", "param4", 5) ])
181

182
    def test024AssignNonCrustToCrust(self):
183
        self._generic_test("unitest/test24.c", [ (crust.crust.MSG_CRITICAL, "Assigning the non-crust variable '{:s}' to the crust variable '{:s}' at line {:d}", "param4", "param5", 5) ])
184

185
    def test025AssignCrustToNonCrust(self):
186
        self._generic_test("unitest/test25.c", [ (crust.crust.MSG_CRITICAL, "Assigning the crust variable '{:s}' to the non-crust variable '{:s}' at line {:d}", "param4", "param5", 5) ])
187

188
    def test026TypeCast(self):
189
        self._generic_test("unitest/test26.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 6, 8) ])
190

191
    def test027AssignNonCrustToCrust2(self):
192
        self._generic_test("unitest/test27.c", [ (crust.crust.MSG_CRITICAL, "Assigning a non-crust value to the crust variable '{:s}' at line {:d}", "param", 5), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 5, 7) ])
193

194
    def test028CrustRetVal(self):
195
        self._generic_test("unitest/test28.c", [ (crust.crust.MSG_ERROR, "Calling the function '{:s}' at line {:d}, but ignoring the crust-type return value", "function", 7) ])
196

197
    def test029UseUninitialized2(self):
198
        self._generic_test("unitest/test29.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} isn't initialized", 1, "function", 7) ])
199

200
    def test030UseFreed2(self):
201
        self._generic_test("unitest/test30.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 6, 5) ])
202

203
    def test031PassedNonCrustyToCrusty2(self):
204
        self._generic_test("unitest/test31.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable", 1, "function", 8) ])
205

206
    def test032PassedCrustyToNonCrusty2(self):
207
        self._generic_test("unitest/test32.c", [ (crust.crust.MSG_CRITICAL, "Expected a non __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable", 1, "function", 6) ])
208

209
    def test033PassedOtherToCrusty2(self):
210
        self._generic_test("unitest/test33.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}", 1, "function", 6) ])
211

212
    def test034MultiAssign(self):
213
        self._generic_test("unitest/test34.c", [ (crust.crust.MSG_CRITICAL, "Assigning a non-crust value to the crust variable '{:s}' at line {:d}", "b", 6), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "a", 6, 8)] )
Sergio Costas's avatar
Sergio Costas committed
214

215
    def test035AmpPointer(self):
216
        self._all_fine_test("unitest/test35.c")
217

218
    def test036StructElement(self):
219 220
        self._all_fine_test("unitest/test36.c")

221
    def test037StructAssign(self):
222
        self._generic_test("unitest/test37.c", [ (crust.crust.MSG_CRITICAL, "Assigning the non-crust variable '{:s}' to the crust variable '{:s}' at line {:d}", "test", "param4", 10) ])
223

224
    def test038ArrayElement(self):
225 226
        self._all_fine_test("unitest/test38.c")

227
    def test039ArrayAssign(self):
228
        self._generic_test("unitest/test39.c", [ (crust.crust.MSG_CRITICAL, "Assigning the non-crust variable '{:s}' to the crust variable '{:s}' at line {:d}", "test", "param4", 6) ])
229

230
    def test040StructAssignment(self):
Sergio Costas's avatar
Sergio Costas committed
231 232
        self._all_fine_test("unitest/test40.c")

233
    def test041UsePostStructAssignment(self):
234
        self._generic_test("unitest/test41.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 9, 8) ])
Sergio Costas's avatar
Sergio Costas committed
235

236
    def test042UsePostStructAssignment2(self):
237
        self._generic_test("unitest/test42.c", [ (crust.crust.MSG_CRITICAL, "Assigning the crust variable '{:s}' to the non-crust variable '{:s}' at line {:d}", 'param3', "test", 8) ])
Sergio Costas's avatar
Sergio Costas committed
238

239
    def test043StructAssignmentByRef(self):
240 241
        self._all_fine_test("unitest/test43.c")

242
    def test044IfEqNull(self):
243 244
        self._generic_test("unitest/test44.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 5, 14), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 10, 14), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var2", 7, 14) ] )

245

246
    def test045IfNoEqNull(self):
247 248
        self._all_fine_test("unitest/test45.c")

249
    def test046IfElseNull(self):
250
        self._generic_test("unitest/test46.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var2", 7, 14) ])
Sergio Costas's avatar
Sergio Costas committed
251

252
    def test047Recycle(self):
253
        lib = self._all_fine_test("unitest/test47.c")
254 255
        self._check_var_state(lib, "var3", crust.crust.VALUE_NOT_NULL, 0)
        self._check_var_state(lib, "var3", crust.crust.VALUE_FREED, 1)
256

257
    def test048Borrow(self):
258
        lib = self._all_fine_test("unitest/test48.c")
259
        self._check_var_state(lib, "var2", crust.crust.VALUE_NOT_NULL)
260

261
    def test049Borrow2(self):
262
        lib = self._all_fine_test("unitest/test49.c")
263
        self._check_var_state(lib, "var2", crust.crust.VALUE_NULL)
264

265
    def test050PassBorrowToNonBorrow(self):
266
        self._generic_test("unitest/test50.c", [ (crust.crust.MSG_CRITICAL, "Argument '{:s}' at position {:d} when calling function '{:s}' at line {:d} is borrowed, but is used as a non-borrow argument", 'param', 1, "test_function", 8) ])
267

268
    def test051PassBorrowToBorrow(self):
269 270
        self._all_fine_test("unitest/test51.c")

271
    def test052PassBorrowToBorrow2(self):
272 273
        self._all_fine_test("unitest/test52.c")

274
    def test053AssignFunctionToFunctionPointerLocal(self):
275 276
        self._all_fine_test("unitest/test53.c")

277
    def test054AssignFunctionToFunctionPointerGlobal(self):
278 279
        self._all_fine_test("unitest/test54.c")

280
    def test055AssignFunctionPointerToFunctionName(self):
281
        self._generic_test("unitest/test55.c", [ (crust.crust.MSG_CRITICAL, "Trying to assign a value to the function name '{:s}' at line {:d}", 'test_function', 10) ])
282

283
    def test056ComparisonDoesntFreePointer(self):
284
        self._generic_test("unitest/test56.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param1", 5, 3) ])
285

286
    def test057ComparisonWithoutBraces(self):
287 288
        self._all_fine_test("unitest/test57.c")

289
    def test058NestedComparisonWithoutBraces(self):
290 291
        self._all_fine_test("unitest/test58.c")

292
    def test059NestedComparisonWithBraces(self):
293 294
        self._all_fine_test("unitest/test59.c")

295
    def test060IfWithNot(self):
296 297
        self._all_fine_test("unitest/test60.c")

298
    def test061IfWithNot2(self):
299
        self._generic_test("unitest/test61.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 11, 7) ])
300

301
    def test062Return(self):
Sergio Costas's avatar
Sergio Costas committed
302 303
        self._all_fine_test("unitest/test62.c")

304
    def test063BlockInUseAtEnd(self):
305
        self._generic_test("unitest/test63.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 5, 8) ])
306

307
    def test064BlockInUseAtEnd2(self):
308
        self._generic_test("unitest/test64.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 5, 7) ])
309

310
    def test065BlockInUseWithReturn(self):
311
        self._generic_test("unitest/test65.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var2", 8, 9) ])
312

313
    def test066BlockInUseWithReturn2(self):
314
        self._generic_test("unitest/test66.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var2", 9, 12) ])
315

316
    def test067BlockInUseWithReturn3(self):
317 318
        self._all_fine_test("unitest/test67.c")

319
    def test068AssignmentToBorrowBlock(self):
320
        self._generic_test("unitest/test68.c", [(crust.crust.MSG_ERROR, "Overwritting the borrowed argument '{:s}' at line {:d}", "param", 7) ])
321

322
    def test069AssignmentFromBorrowBlock(self):
323
        self._generic_test("unitest/test69.c", [ (crust.crust.MSG_ERROR, "Assigning a borrowed block into a local variable is not allowed (assigning '{:s}' at line {:d})", "param", 9) ])
324

325
    def test070AssignmentFromBorrowBlock2(self):
326
        self._generic_test("unitest/test70.c", [ (crust.crust.MSG_ERROR, "Assigning a borrowed block into a local variable is not allowed (assigning '{:s}' at line {:d})", "param", 9) ])
327

328
    def test071AssignmentToAndFromMemberOfBorrowBlock(self):
329
        self._generic_test("unitest/test71.c", [ (crust.crust.MSG_WARNING, "Using variable '{:s}' at line {:d} with a possible NULL value", "param", 10) ])
330

331
    def test072AliasFreesBlock(self):
332
        lib = self._all_fine_test("unitest/test72.c")
333 334
        self._check_var_state(lib, "param", crust.crust.VALUE_FREED)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_FREED)
335

336
    def test073AliasFreesBlock2(self):
337
        self._generic_test("unitest/test73.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 9, 8) ])
338

339
    def test074BlockFreesAlias(self):
340
        self._generic_test("unitest/test74.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 10, 9) ])
341

342
    def test075DecoupleAlias(self):
343
        lib = self._generic_test("unitest/test75.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 13, 9) ])
344 345 346
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_FREED, 1)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_NULL, 2)
347

348

349
    def test076DecoupleAlias2(self):
350
        lib = self._all_fine_test("unitest/test76.c")
351 352 353 354 355
        self._check_var_state(lib, "param1", crust.crust.VALUE_FREED, 0)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "param2", crust.crust.VALUE_FREED, 1)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_FREED, 1)
356

357
    def test077DecoupleAlias3(self):
358
        lib = self._generic_test("unitest/test77.c", [ (crust.crust.MSG_ERROR, "Assigning the crust-type result value of function '{:s}' to the alias '{:s}' at line {:d}", "function", "an_alias", 11) ])
359 360
        self._check_var_state(lib, "param", crust.crust.VALUE_FREED, 1)
        self._check_var_state(lib, "an_alias", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
361

362
    def test078FunctionPointerNull(self):
363
        self._generic_test("unitest/test78.c", [ (crust.crust.MSG_ERROR, "Using function pointer '{:s}' at line {:d} with NULL value", "test1", 9) ])
364

365
    def test079FunctionPointerNull2(self):
366
        self._generic_test("unitest/test79.c", [ (crust.crust.MSG_ERROR, "Using function pointer '{:s}' at line {:d} with NULL value", "test1", 7) ])
367

368
    def test080FunctionPointerFine(self):
369 370 371
        self._all_fine_test("unitest/test80.c")


372
    def test081FunctionPointerFine2(self):
373 374
        self._all_fine_test("unitest/test81.c")

375
    def test082FunctionPointerNonInitialized(self):
376
        self._generic_test("unitest/test82.c", [ (crust.crust.MSG_ERROR, "Using uninitialized function pointer '{:s}' at line {:d}", "test1", 9) ])
377

378
    def test083FunctionPointerNonInitialized2(self):
379
        self._generic_test("unitest/test83.c", [ (crust.crust.MSG_ERROR, "Using uninitialized function pointer '{:s}' at line {:d}", "test1", 7) ])
380

381
    def test084UndefinedPointerFunction(self):
382
        self._raise_test("unitest/test84.c", crust.crust.FunctionNotFoundException, [ (crust.crust.MSG_CRITICAL, "Calling function '{:s}' at line {:d}, but it is not declared or defined", "test1", 6) ])
383

384
    def test085FunctionPointerIncorrectNumberOfParameters(self):
385
        self._generic_test("unitest/test85.c", [ (crust.crust.MSG_CRITICAL, "Calling function '{:s}' at line {:d} with an incorrect number of arguments", "test1", 9) ])
386

387
    def test086FunctionPointerIncorrectNumberOfParameters2(self):
388
        self._generic_test("unitest/test86.c", [ (crust.crust.MSG_CRITICAL, "Calling function '{:s}' at line {:d} with an incorrect number of arguments", "test1", 7) ])
389

390
    def test087FunctionPointerExpectedCrustParameter(self):
391
        self._generic_test("unitest/test87.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable", 1, "test1", 10) ])
392

393
    def test088FunctionPointerExpectedCrustParameter2(self):
394
        self._generic_test("unitest/test88.c", [ (crust.crust.MSG_CRITICAL, "Expected a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable", 1, "test1", 8) ])
395

396
    def test089FunctionPointerExpectedNonCrustParameter(self):
397
        self._generic_test("unitest/test89.c", [ (crust.crust.MSG_CRITICAL, "Expected a non __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable", 1, "test1", 11) ])
398

399
    def test090FunctionPointerExpectedNonCrustParameter2(self):
400
        self._generic_test("unitest/test90.c", [ (crust.crust.MSG_CRITICAL, "Expected a non __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable", 1, "test1", 9) ])
401

402
    def test091NestedVariables(self):
403
        lib = self._all_fine_test("unitest/test91.c")
404 405
        self._check_var_state(lib, "tmp1", crust.crust.VALUE_UNINITIALIZED, 1)
        self._check_var_state(lib, "tmp1", crust.crust.VALUE_NULL, 0)
406

407
    def test092FunctionPointerFineNoParams(self):
408 409
        self._all_fine_test("unitest/test92.c")

410
    def test093FunctionPointerFineNoParams2(self):
411 412
        self._all_fine_test("unitest/test93.c")

413
    def test094FunctionPointerReturnsCrust(self):
414
        self._generic_test("unitest/test94.c", [ (crust.crust.MSG_ERROR, "Calling the function '{:s}' at line {:d}, but ignoring the crust-type return value", "test1", 11) ])
415

416
    def test095FunctionPointerReturnsCrust2(self):
417
        self._generic_test("unitest/test95.c", [ (crust.crust.MSG_ERROR, "Calling the function '{:s}' at line {:d}, but ignoring the crust-type return value", "test1", 9) ])
418

419
    def test096BreakOutsideLoop(self):
420
        self._generic_test("unitest/test96.c", [ (crust.crust.MSG_CRITICAL, "Break not in a loop at line {:d}", 2) ])
421

422
    def test097WhileFreesTwice(self):
423
        self._generic_test("unitest/test97.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 10, 10) ])
424

425
    def test098AndOpFine(self):
426 427
        self._all_fine_test("unitest/test98.c")

428
    def test099OrOpFine(self):
429 430
        self._all_fine_test("unitest/test99.c")

431
    def test100AndOpWrong(self):
432
        self._generic_test("unitest/test100.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param1", 10, 7) ])
433

434
    def test101OrOpWrong(self):
435
        self._generic_test("unitest/test101.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param2", 11, 7) ])
436

437
    def test102WhileCrustVarFine(self):
438
        self._all_fine_test("unitest/test102.c")
439

440
    def test103WhileWithoutNoZero(self):
441
        self._generic_test("unitest/test103.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} isn't initialized", 1, "function", 15) ])
442

443
    def test104WhileWithNoZeroFine(self):
444 445
        self._all_fine_test("unitest/test104.c")

446 447 448 449
    def test105DifferenParameterNumberNoCrust(self):
        self._all_fine_test("unitest/test105.c")

    def test106DifferenParameterNumberCrust(self):
450
        self._generic_test("unitest/test106.c", [ (crust.crust.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but they have a different number of arguments", "function", "test1", 10) ])
451 452

    def test107DifferenParameterNumberCrust(self):
453
        self._generic_test("unitest/test107.c", [ (crust.crust.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but they have a different number of arguments", "function", "test1", 10) ])
454

455
    def test108DifferenParameterTypes(self):
456
        self._generic_test("unitest/test108.c", [ (crust.crust.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but argument {:d} differs", "function", "test1", 10, 1) ])
457 458

    def test109DifferenParameterTypes(self):
459
        self._generic_test("unitest/test109.c", [ (crust.crust.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but the return types differ", "function", "test1", 10) ])
460 461 462 463

    def test110ParameterTypesMatch(self):
        self._all_fine_test("unitest/test110.c")

464 465 466 467 468 469 470 471 472
    def test111CommaOperatorFine(self):
        self._all_fine_test("unitest/test111.c")

    def test112CommaOperatorBad(self):
        self._generic_test("unitest/test112.c", [ (crust.crust.MSG_ERROR, "Calling the function '{:s}' at line {:d}, but ignoring the crust-type return value", "function", 12), (crust.crust.MSG_CRITICAL, "Assigning the non-crust variable '{:s}' to the crust variable '{:s}' at line {:d}", "b", "param", 12) ])

    def test113CommaOperatorFine2(self):
        self._all_fine_test("unitest/test113.c")

473 474 475
    def test114CheckSelfAssignOps(self):
        self._all_fine_test("unitest/test114.c")

476 477 478
    def test115VariableNumberOfArguments(self):
        self._generic_test("unitest/test115.c", [ (crust.crust.MSG_CRITICAL, "Passed a __crust_t__ variable as argument {:d} when calling function '{:s}' at line {:d}, but __crust_t__ variables are not allowed for optional arguments in functions with variable number of arguments", 3, "function", 11) ])

479 480 481 482 483 484 485 486 487
    def test116NonCrustRetValToCrust(self):
        self._generic_test("unitest/test116.c", [ (crust.crust.MSG_ERROR, "Assigning the non-crust-type result value of function '{:s}' at line {:d} to the crust variable '{:s}'", "function", 5, "retval") ])

    def test117CrustRetValToNonCrust(self):
        self._generic_test("unitest/test117.c", [ (crust.crust.MSG_ERROR, "Assigning the crust-type result value of function '{:s}' at line {:d} to the non-crust variable '{:s}'", "function", 5, "retval") ])

    def test118CallingPointerWithPosibleNull(self):
        self._generic_test("unitest/test118.c", [ (crust.crust.MSG_WARNING, "Using function pointer '{:s}' at line {:d} with a possible NULL value", "test1", 11) ])

488 489
    def test119BlockToGlobalVar(self):
        self._all_fine_test("unitest/test119.c")
490

491
    def test120DoWhileUsesTwice(self):
492
        self._generic_test("unitest/test120.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 10, 10) ])
493 494 495 496 497 498 499

    def test121DoWhileExitsFine(self):
        self._all_fine_test("unitest/test121.c")

    def test122DoWhileExitsBreak(self):
        self._all_fine_test("unitest/test122.c")

500
    def test123ForDefinedOutside(self):
501
        self._generic_test("unitest/test123.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 10, 10) ])
502 503

    def test124ForDefinedInside(self):
504
        self._generic_test("unitest/test124.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 8, 8) ])
505 506

    def test125ForDefinedOutside(self):
507
        self._generic_test("unitest/test125.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "main", 10, 10) ])
508 509 510 511 512

    def test126ForFreesBlocks(self):
        self._all_fine_test("unitest/test126.c")

    def test127ForProcessesBlocks(self):
513
        self._generic_test("unitest/test127.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 7, 13) ])
514

515 516
    def test128AssignAliasFromAlias(self):
        lib = self._all_fine_test("unitest/test128.c")
517 518 519
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_UNINITIALIZED, 0)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 0)
Sergio Costas's avatar
Sergio Costas committed
520

521

522 523 524
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 1)
525

526 527 528
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
529 530 531

    def test129AssignAliasFromAlias2(self):
        lib = self._all_fine_test("unitest/test129.c")
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_UNINITIALIZED, 0)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 0)

        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 1)

        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)

        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
551 552 553

    def test130AssignAliasFromAlias(self):
        lib = self._all_fine_test("unitest/test130.c")
554 555 556
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_UNINITIALIZED, 0)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 0)
557

558 559 560
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 1)
561

562 563 564
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
565

566 567 568
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 3)
569

570 571 572
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL, 4)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_FREED, 4)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL, 4)
573

574 575
    def test131AssignAliasFromAlias(self):
        lib = self._all_fine_test("unitest/test131.c")
576 577 578
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_UNINITIALIZED, 0)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 0)
579

580 581 582
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 1)
583

584 585 586
        self._check_var_state(lib, "param", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
587

588 589 590
        self._check_var_state(lib, "param", crust.crust.VALUE_FREED, 3)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_FREED, 3)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_FREED, 3)
591 592 593

    def test132AssignAliasFromAlias(self):
        lib = self._all_fine_test("unitest/test132.c")
594 595 596 597 598 599 600 601 602
        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_UNINITIALIZED, 0)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 0)

        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 1)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_UNINITIALIZED, 1)
603
        self._check_block_id_is_the_same(lib, "param1", "alias1", 1)
604

605 606 607 608
        self._check_var_state(lib, "param1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL_OR_NULL, 2)
609 610
        self._check_block_id_is_the_same(lib, "param1", "alias1", 2)
        self._check_block_id_is_the_same(lib, "param2", "alias2", 2)
611

612 613 614 615
        self._check_var_state(lib, "param1", crust.crust.VALUE_FREED, 3)
        self._check_var_state(lib, "param2", crust.crust.VALUE_NOT_NULL, 3)
        self._check_var_state(lib, "alias1", crust.crust.VALUE_FREED, 3)
        self._check_var_state(lib, "alias2", crust.crust.VALUE_NOT_NULL, 3)
616

617
    def test133GotoJumpsOverAll(self):
618
        self._generic_test("unitest/test133.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 12, 21) ])
619

620
    def test134ReturnCrustExpectCrust(self):
621
        self._all_fine_test("unitest/test134.c")
622 623

    def test135ReturnNoCrustExpectNoCrust(self):
624
        self._all_fine_test("unitest/test135.c")
625 626 627 628 629 630 631

    def test136ReturnCrustExpectNoCrust(self):
        self._generic_test("unitest/test136.c", [ (crust.crust.MSG_ERROR, "Return statement at line {:d} is returning a crust value, but is must return a non-crust one", 7) ])

    def test137ReturnNoCrustExpectCrust(self):
        self._generic_test("unitest/test137.c", [ (crust.crust.MSG_ERROR, "Return statement at line {:d} is returning a non-crust value, but is must return a crust one", 7) ])

632
    def test138OddBreakInFor(self):
633
        self._generic_test("unitest/test138.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param1", 7, 15) ])
634 635

    def test139BreakInNestedFor(self):
636
        self._generic_test("unitest/test139.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param2", 7, 22), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param1", 7, 22) ])
637

638
    def test140Switch1(self):
639
        self._generic_test("unitest/test140.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param1", 7, 27), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param2", 7, 27), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param3", 7, 27) ])
640 641

    def test141Switch2(self):
642
        self._generic_test("unitest/test141.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 25, 13), (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 25, 16), (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 25, 19), (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 25, 22) ])
643 644

    def test142Switc3(self):
645
        self._generic_test("unitest/test142.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 15, 13), (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 15, 11), (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} was freed at line {:d}", 1, "function", 13, 11) ])
646

647 648 649 650 651 652
    def test143CheckSignatures(self):
        self._generic_test("unitest/test143.c", [ (crust.crust.MSG_CRITICAL, "Function definition for '{:s}' at line {:d}, file '{:s}' differs from definition at line {:d}, file '{:s}'", "main", 7, "unitest/test143.c", 5, "unitest/test143.c") ])

    def test144CheckSignatures(self):
        self._generic_test("unitest/test144.c", [ (crust.crust.MSG_CRITICAL, "Function definition for '{:s}' at line {:d}, file '{:s}' differs from definition at line {:d}, file '{:s}'", "main", 7, "unitest/test144.c", 5, "unitest/test144.c") ])

653 654 655 656 657 658 659 660
    def test145CheckAssignAndCallPointerToPossibleNull(self):
        self._generic_test("unitest/test145.c", [ (crust.crust.MSG_WARNING, "Using variable '{:s}' at line {:d} with a possible NULL value", "param1", 14), (crust.crust.MSG_WARNING, "Using variable '{:s}' at line {:d} with a possible NULL value", "param1", 15) ])

    def test146CheckAssignToElementOfNullPointer(self):
        self._generic_test("unitest/test146.c", [ (crust.crust.MSG_ERROR, "Using variable '{:s}' at line {:d}, but it has been already freed", "param1", 15) ])

    def test147CheckAssignToElementOfUninitializedPointer(self):
        self._generic_test("unitest/test147.c", [ (crust.crust.MSG_ERROR, "Using variable '{:s}' at line {:d}, but it hasn't been initialized yet", "param1", 15) ])
661

662 663 664
    def test148CheckStructAccessWithoutPointer(self):
        self._all_fine_test("unitest/test148.c")

Sergio Costas's avatar
Sergio Costas committed
665
    def test149TestWhileContinue(self):
666
        self._generic_test("unitest/test149.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 12, 16), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])
Sergio Costas's avatar
Sergio Costas committed
667 668

    def test150TestDoWhileContinue(self):
669
        self._generic_test("unitest/test150.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 12, 16), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])
Sergio Costas's avatar
Sergio Costas committed
670 671

    def test151TestForContinue(self):
672
        self._generic_test("unitest/test151.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param", 12, 16), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])
Sergio Costas's avatar
Sergio Costas committed
673 674

    def test152TestWhileContinue2(self):
675
        self._generic_test("unitest/test152.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} isn't initialized", 1, "function", 16), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])
Sergio Costas's avatar
Sergio Costas committed
676 677 678 679 680

    def test153TestDoWhileContinue2(self):
        self._generic_test("unitest/test153.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])

    def test154TestForContinue2(self):
681
        self._generic_test("unitest/test154.c", [ (crust.crust.MSG_ERROR, "Argument {:d} when calling function '{:s}' at line {:d} isn't initialized", 1, "function", 16), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])
Sergio Costas's avatar
Sergio Costas committed
682 683 684 685 686 687 688

    def test155TestWhileContinue3(self):
        self._generic_test("unitest/test155.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])

    def test156TestForContinue3(self):
        self._generic_test("unitest/test156.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param", 12, 12) ])

689 690 691
    def test157CheckEnums(self):
        self._all_fine_test("unitest/test157.c")

692 693 694 695 696 697 698
    def test158TestBorrowVariables(self):
        self._generic_test("unitest/test158.c", [ (crust.crust.MSG_CRITICAL, "Global variable '{:s}', defined as __crust_borrow__ in line {:d}, but that is not allowed", "global_var", 1) ])

    def test159TestBorrowVariables2(self):
        self._generic_test("unitest/test159.c", [ (crust.crust.MSG_ERROR, "Assigning the non-borrowed block '{:s}' to the borrowed variable '{:s}' at line {:d}", "param2", "var2", 12), (crust.crust.MSG_ERROR, "Assigning a value to the borrowed variable '{:s}' at line {:d}", "var4", 14) ])

    def test160AssignRetBorrowToNonBorrow(self):
699
        self._generic_test("unitest/test160.c", [ (crust.crust.MSG_ERROR, "Assigning the borrowed result value of function '{:s}' to the non-borrowed variable '{:s}' at line {:d}", "function1", "var1", 11), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var1", 11, 12) ])
700 701

    def test161AssignRetNonBorrowToNonBorrow(self):
702
        self._generic_test("unitest/test161.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var1", 11, 12) ])
703 704 705 706 707 708 709 710

    def test162AssignRetBorrowToBorrow(self):
        self._all_fine_test("unitest/test162.c")

    def test163AssignRetBorrowToNonBorrow(self):
        self._generic_test("unitest/test163.c", [ (crust.crust.MSG_ERROR, "Assigning the non-borrowed result value of function '{:s}' to the borrowed variable '{:s}' at line {:d}", "function1", "var1", 11) ])

    def test164AssignRetBorrowToBorrowParam(self):
711
        self._generic_test("unitest/test164.c", [ (crust.crust.MSG_ERROR, "Overwritting the borrowed argument '{:s}' at line {:d}", "param1", 9) ])
712 713 714 715 716 717 718 719 720 721 722 723 724

    def test165ExpectedReturnValueButNoValueAtEnd(self):
        self._generic_test("unitest/test165.c", [ (crust.crust.MSG_ERROR, "Function '{:s}' expects a return value, but the code exited without it", "main") ])

    def test166ExpectedNoReturnValueButValueAvailable(self):
        self._generic_test("unitest/test166.c", [ (crust.crust.MSG_ERROR, "Return statement at line {:d} is returning a value, but the function '{:s}' does not return a value", 7, "main") ])

    def test167ExpectedReturnValueEmptyReturn(self):
        self._generic_test("unitest/test167.c", [ (crust.crust.MSG_ERROR, "Function '{:s}' expects a return value, but the code exited at line {:d} without it", "main", 9) ])

    def test168ExpectedReturnValueEmptyReturn(self):
        self._generic_test("unitest/test168.c", [ (crust.crust.MSG_ERROR, "Function '{:s}' expects a borrowed value to return, but a non-borrowed was used at line {:d}", "function1", 6) ])

725 726 727 728 729 730
    def test169ReturnedFreedValue(self):
        self._generic_test("unitest/test169.c", [ (crust.crust.MSG_ERROR, "Returning variable '{:s}' at line {:d} was freed at line {:d}", "param", 9, 8) ])

    def test170ReturnedUninitializedValue(self):
        self._generic_test("unitest/test170.c", [ (crust.crust.MSG_ERROR, "Returning variable '{:s}' at line {:d} is uninitialized", "param", 9) ])

731 732 733
    def test171IgnoreBorrowReturnValue(self):
        self._all_fine_test("unitest/test171.c")

734
    def test172AssignCrustToNoCrustThroughTypecast(self):
735
        self._generic_test("unitest/test172.c", [ (crust.crust.MSG_WARNING, "Assigning, with a typecast, the crust-type result value of function '{:s}' at line {:d} to the non-crust variable '{:s}'", "function1", 11, "var1") ])
736

737 738 739 740
    def test173TestNoNullParameter(self):
        self._all_fine_test("unitest/test173.c")

    def test174AssignCrustToNoCrustThroughTypecast(self):
741
        self._generic_test("unitest/test174.c", [ (crust.crust.MSG_WARNING, "Argument '{:s}' at position {:d} when calling function '{:s}' at line {:d} is defined as not_null, but is being called with a possible NULL value", "param", 1, "function1", 9) ])
742 743 744 745 746 747 748

    def test175AssignCrustToNoCrustThroughTypecast(self):
        self._generic_test("unitest/test175.c", [ (crust.crust.MSG_ERROR, "Passing NULL as argument {:d} when calling function '{:s}' at line {:d}, but it must be a not_null value", 1, "function1", 9) ])

    def test176AssignCrustToNoCrustThroughTypecast(self):
        self._generic_test("unitest/test176.c", [ (crust.crust.MSG_ERROR, "Argument '{:s}' at position {:d} when calling function '{:s}' at line {:d} is defined as not_null, but is being called with a NULL value", "param", 1, "function1", 10) ])

749 750 751 752 753 754 755 756
    def test177TestNoNullParameter(self):
        self._all_fine_test("unitest/test177.c")

    def test178AssignCrustToNoCrustThroughTypecast(self):
        self._generic_test("unitest/test178.c", [ (crust.crust.MSG_ERROR, "Return value at line {:d} is NULL, but the function must not return a NULL value", 7) ])

    def test179AssignCrustToNoCrustThroughTypecast(self):
        self._generic_test("unitest/test179.c", [ (crust.crust.MSG_ERROR, "Return value at line {:d} is NULL, but the function must not return a NULL value", 7) ])
757

758 759 760
    def test180ReturnFunctionReturn(self):
        self._all_fine_test("unitest/test180.c")

761 762 763 764 765 766
    def test181CheckIfWithUninitializedVariable(self):
        self._generic_test("unitest/test181.c", [ (crust.crust.MSG_ERROR, "Using uninitialized variable '{:s}' at line {:d}", "param1", 7) ])

    def test182CheckCompareWithNullWhenFreed(self):
        self._all_fine_test("unitest/test182.c")

767
    def test183CheckIfWithUninitializedVariable(self):
768
        self._generic_test("unitest/test183.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param3", 5, 9) ])
769

770
    def test184CheckAssignToSeveralGlobalVars(self):
771
        lib = self._generic_test("unitest/test184.c", [ (crust.crust.MSG_ERROR, "At exit point in line {:d}, global variable '{:s}' points to the same block than global variable '{:s}'.", 18, "global_var1", "global_var2") ])
772 773
        self._check_block_id_is_the_same(lib, "param1", "global_var1")
        self._check_block_id_is_the_same(lib, "param1", "global_var2")
774

775
    def test185CheckAssignToSeveralGlobalVars2(self):
776
        lib = self._generic_test("unitest/test185.c", [ (crust.crust.MSG_ERROR, "At exit point in line {:d}, global variable '{:s}' points to the same block than global variable '{:s}'.", 20, "global_var1", "global_var2") ])
777 778
        self._check_block_id_is_the_same(lib, "param1", "global_var1")
        self._check_block_id_is_the_same(lib, "param1", "global_var2")
779

780
    def test186CheckIfWithUninitializedVariable(self):
781
        self._generic_test("unitest/test186.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "data", 7, 11) ])
782

783 784 785
    def test187GlobalVarsAlwaysNotNull(self):
        self._all_fine_test("unitest/test187.c")

786
    def test188GlobalAssignment(self):
787 788 789 790
        lib = self._all_fine_test("unitest/test188.c")

    def test189AssignmentWithAsterisk(self):
        lib = self._all_fine_test("unitest/test189.c")
791 792
        self._check_var_state(lib, "var1", crust.crust.VALUE_NOT_NULL_OR_NULL)
        self._check_var_state(lib, "var2", crust.crust.VALUE_NOT_NULL_OR_NULL)
793 794 795

    def test190FunctionWithVoidParameter(self):
        self._all_fine_test("unitest/test190.c")
796

797 798 799 800 801
    def test191CheckAssignmentAfterAssignmentToGlobal(self):
        lib = self._all_fine_test("unitest/test191.c")
        self._check_block_id_is_the_same(lib, "arg1", "var1", 0)
        self._check_block_id_is_not_same(lib, "arg1", "var1", 1)

802 803

    def test192GlobalVariableFreedAtExit(self):
804
        self._generic_test("unitest/test192.c", [ (crust.crust.MSG_ERROR, "At exit point in line {:d}, global variable '{:s}' points to a block freed at line {:d}.", 14, "var1", 12) ])
805

806
    def test193GlobalVariableFreedAtExit(self):
807
        lib = self._generic_test("unitest/test193.c", [ (crust.crust.MSG_ERROR, "At exit point in line {:d}, global variable '{:s}' points to the same block than global variable '{:s}'.", 18, "var1", "var2") ])
808 809 810 811 812
        self._check_block_id_is_the_same(lib, "arg1", "var1")
        self._check_block_id_is_the_same(lib, "arg1", "var2")

    def test194GlobalVariableFreedAtExit(self):
        lib = self._all_fine_test("unitest/test194.c")
813
        self._check_var_state(lib, "global_var", crust.crust.VALUE_NOT_NULL_OR_NULL)
814

815 816 817 818 819 820 821 822
    def test195ForLoopWithoutFreeing(self):
        self._generic_test("unitest/test195.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "var", 12, 9), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "var", 12, 12) ])

    def test196ForLoopWithoutFreeing2(self):
        self._generic_test("unitest/test196.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "var", 14, 11), (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "var", 14, 14) ])

    def test197ForLoopFreeing(self):
        self._all_fine_test("unitest/test197.c")
823

824
    def test198ForLoopWithoutFreeingWithAlias(self):
825
        self._generic_test("unitest/test198.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "var", 13, 19) ])
826

827 828 829
    def test199AssingFunctionPointerToAlias(self):
        self._generic_test("unitest/test199.c", [ (crust.crust.MSG_ERROR, "Trying to assign the function '{:s}' into the variable '{:s}', which is not a pointer to function, at line {:d}", "function2", "tmp1", 16), (crust.crust.MSG_ERROR, "Assigning a pointer of the function '{:s}' to the alias '{:s}' at line {:d}", "function2", "tmp1", 16) ])

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    def test200GlobalVariableFreedAtExit2(self):
        lib = self._all_fine_test("unitest/test200.c")
        self._check_var_state(lib, "global_var", crust.crust.VALUE_NOT_NULL_OR_NULL)

    def test201TestSetNull(self):
        lib = self._all_fine_test("unitest/test201.c")
        self._check_var_state(lib, "var1", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "var1", crust.crust.VALUE_NULL, 1)

    def test202TestSetNotNull(self):
        lib = self._generic_test("unitest/test202.c", [ (crust.crust.MSG_ERROR, "Assignment to '{:s}' at line {:d}, but it is not freed", "var1", 13) ])
        self._check_var_state(lib, "var1", crust.crust.VALUE_NOT_NULL_OR_NULL, 0)
        self._check_var_state(lib, "var1", crust.crust.VALUE_NOT_NULL, 1)

    def test203TestSetNullNonGlobal(self):
        lib = self._generic_test("unitest/test203.c", [ (crust.crust.MSG_CRITICAL, "Trying to set to NULL the state of the non-global variable '{:s}' at line {:d}", "var1", 7) ])

    def test204TestSetNullNonGlobal(self):
        lib = self._generic_test("unitest/test204.c", [ (crust.crust.MSG_CRITICAL, "Trying to set to NOT_NULL the state of the non-global variable '{:s}' at line {:d}", "var1", 7) ])

850 851 852 853 854 855
    def test205SwitchWithArrayItem(self):
        self._all_fine_test("unitest/test205.c")

    def test206SwitchWithPointerContent(self):
        self._all_fine_test("unitest/test206.c")

856 857 858 859
    def test207SwitchWithPointerContent(self):
        lib = self._all_fine_test("unitest/test207.c")
        self._check_var_state(lib, "str", crust.crust.VALUE_NOT_NULL)

860 861 862 863
    def test208TestSetNullNonGlobal(self):
        lib = self._generic_test("unitest/test208.c", [ (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param2", 7, 15), (crust.crust.MSG_ERROR, "Memory block '{:s}', initialized at line {:d}, is still in use at exit point in line {:d}", "param1", 7, 15) ])
        self._check_var_state(lib, "a", crust.crust.VALUE_NOT_NULL_OR_NULL)

864 865 866
    def test209TestSetNullNonGlobal(self):
        lib = self._generic_test("unitest/test209.c", [ (crust.crust.MSG_CRITICAL, "Function definition for '{:s}' at line {:d}, file '{:s}' differs from definition at line {:d}, file '{:s}'", "function", 9, "unitest/test209.c", 6, "unitest/test209.c") ])

Sergio Costas's avatar
Sergio Costas committed
867
if __name__ == '__main__':
868 869 870 871
    try:
        os.remove("error_list.txt")
    except:
        pass
Sergio Costas's avatar
Sergio Costas committed
872
    unittest.main()