-
Notifications
You must be signed in to change notification settings - Fork 1
/
test2.html
111 lines (102 loc) · 3.29 KB
/
test2.html
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
<!doctype HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsContract</title>
<script type="text/javascript" src="jsContract.js">
</script>
<script type="text/javascript">
function run(instrumented, code){
if (instrumented) {
code = Contract.instrument(code);
}
eval(code);
}
</script>
</head>
<body>
<h1>jsContract</h1>
Two pieces of code is shown here, one with code contracts and one without. <br/>
Both pieces of code look correct right? And when you run the regular code, you DO get a result right? And there is no errors thrown, right?<br/>
But is the produced result correct???<br/><br/>
Try the different samples and see if you can spot the bug!<br/>
Number of fibonacci nubers to compute <input type="text" value="10" id="length"/> 5 more numbers will be added in code.
<br/>
<textarea id="code1" style="width:49%;height:400px">
function fibonacci(length){
//Copied from http://sonic.net/~douglasi/fibo.htm
//Script by Doug McKenzie email: douglasi@sonic.net
//Use as you wish but leave the above line in place, thanks.
//Modified by Øyvind Sean Kinsey for use as an example
var results = [0, 1];
//we actually want to add an extra 5 numbers to this
length = length + 5;
var n = 0;
var x = 1;
var i = 0;
for (n = 1; n <
length; n++) {
x = x + i;
i = x - i;
results.push(x)
}
return results;
}
function getLength(){
return document.getElementById("length").value;
}
function doComputation(length){
var result = fibonacci(length);
alert(result.join());
}
doComputation(getLength());
</textarea>
<textarea id="code2" style="width:49%;height:400px">
function fibonacci(length){
Contract.expectNumber(length);
Contract.expect(length < 100);
//Copied from http://sonic.net/~douglasi/fibo.htm
//Script by Doug McKenzie email: douglasi@sonic.net
//Use as you wish but leave the above line in place, thanks.
//Modified by Øyvind Sean Kinsey for use as an example
var results = [0, 1];
//we actually want to add an extra 5 numbers to this
length = length + 5;
var n = 0;
var x = 1;
var i = 0;
for (n = 1; n <
length; n++) {
x = x + i;
i = x - i;
results.push(x)
}
return results;
}
function getLength(){
Contract.guaranteesNumber("the value from the input field was not a number");
return document.getElementById("length").value;
}
function doComputation(length){
Contract.expectNumber(length);
var result = fibonacci(length);
alert(result.join());
}
try {
doComputation(getLength());
}
catch (e) {
alert(e.message);
}
</textarea>
<button onclick="run(false,document.getElementById('code1').value);" style="width:49%">
Run
</button>
<button onclick="run(false,document.getElementById('code2').value);" style="width:24%">
Run without instrumenting
</button>
<button onclick="run(true,document.getElementById('code2').value);" style="width:24%">
Run with instrumention
</button>
</body>
</html>