forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.jl
More file actions
116 lines (101 loc) · 2.66 KB
/
Copy pathtest.jl
File metadata and controls
116 lines (101 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module TestTypeCheck
using TypeCheck, FactCheck
istype(t) = isa(t,TypeCheck.AType)
facts("Check Return Types: Make Sure It Runs on Base") do
for n in names(Base)
if isdefined(Base,n)
f = eval(Base,n)
if isgeneric(f) && typeof(f) == Function
context(string(n)) do
@fact TypeCheck.check_return_types(f) => anything # => FunctionSignature([],Symbol)
[@fact istype(TypeCheck.returntype(e)) => true for e in code_typed(f)]
[@fact TypeCheck.returntype(e) => istype for e in code_typed(f)]
end
end
else
@fact n => x->isdefined(Base,x)
end
end
end
caught(x) = x[2] == true
notcaught(x) = x[2] == false
function check_return(f,check)
@fact length(code_typed(f)) => 1
@fact TypeCheck.check_return_type(code_typed(f)[1]) => check
end
facts("Check Return Types: True Positives") do
barr(x::Int) = isprime(x) ? x : false
check_return(barr, caught)
end
facts("Check Return Types: False Negatives") do
foo(x::Any) = isprime(x) ? x : false
check_return(foo, notcaught)
end
facts("Check Loop Types: Make Sure It Runs on Base") do
for n in names(Base)
if isdefined(Base,n)
f = eval(Base,n)
if isgeneric(f) && typeof(f) == Function
context(string(n)) do
@fact TypeCheck.check_loop_types(f) => anything # => LoopResults
end
end
else
@fact n => x->isdefined(Base,x)
end
end
end
passed(x) = isempty(x.methods)
failed(x) = !passed(x)
function check_loops(f,check)
@fact length(code_typed(f)) => 1
@fact check_loop_types(f) => check
end
facts("Check Loop Types: True Positives") do
function f1(x::Int)
for n in 1:x
x /= n
end
return x
end
check_loops(f1,failed)
end
facts("Check Loop Types: True Negatives") do
function g1()
x::Int = 5
for i = 1:100
x *= 2.5
end
return x
end
check_loops(g1,passed)
function g2()
x = 5
x = 0.2
for i = 1:10
x *= 2
end
return x
end
check_loops(g2,passed)
end
facts("Check Method Calls: Make Sure It Runs on Base") do
for n in names(Base)
if isdefined(Base,n)
f = eval(Base,n)
if isgeneric(f) && typeof(f) == Function
context(string(n)) do
@fact TypeCheck.check_method_calls(f) => anything # => FunctionCalls
end
end
else
@fact n => x->isdefined(Base,x)
end
end
end
facts("Check Method Calls: True Positives") do
end
facts("Check Method Calls: False Negatives") do
end
exitstatus()
end