-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
136 lines (132 loc) · 4.09 KB
/
test.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
tests for Bitwise Logic.
Each operator is checked
*/
/*
run this code, and either:
a) connect a webUSB terminal to the connected microbit, or
b) use the MakeCode simulator's console
then check that all tests result in a P on the terminal/console
*/
function checkAnd (stra: string, strb: string) {
let b = parseInt(stra,16)
let c = parseInt(strb,16)
let jsResult2 = (b & c) & 0xff
let myResult2 = BitwiseLogic.bitwise2arg(b, operator.and,c) & 0xff
serial.writeLine("js: " + decToHex(jsResult2) + " myCode: " + decToHex(myResult2))
if (jsResult2 == myResult2) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkOr (stra: string, strb: string) {
let d = parseInt(stra,16)
let e = parseInt(strb,16)
let jsResult3 = (d | e) & 0xff
let myResult3 = BitwiseLogic.bitwise2arg(d, operator.or,e) & 0xff
serial.writeLine("js: " + decToHex(jsResult3) + " myCode: " + decToHex(myResult3))
if (jsResult3 == myResult3) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkXor (stra: string, strb: string) {
let d = parseInt(stra,16)
let e = parseInt(strb,16)
let jsResult3 = (d ^ e) & 0xff
let myResult3 = BitwiseLogic.bitwise2arg(d, operator.xor,e) & 0xff
serial.writeLine("js: " + decToHex(jsResult3) + " myCode: " + decToHex(myResult3))
if (jsResult3 == myResult3) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkLeftShift (stra: string, strb: string) {
let d = parseInt(stra,16)
let e = parseInt(strb,16)
let jsResult3 = (d << e) & 0xff
let myResult3 = BitwiseLogic.bitwise2arg(d, operator.leftShift,e) & 0xff
serial.writeLine("js: " + decToHex(jsResult3) + " myCode: " + decToHex(myResult3))
if (jsResult3 == myResult3) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkRightShift (stra: string, strb: string) {
let d = parseInt(stra,16)
let e = parseInt(strb,16)
let jsResult3 = (d >> e) & 0xff
let myResult3 = BitwiseLogic.bitwise2arg(d, operator.rightShift,e) & 0xff
serial.writeLine("js: " + decToHex(jsResult3) + " myCode: " + decToHex(myResult3))
if (jsResult3 == myResult3) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkZeroFillRightShift (stra: string, strb: string) {
let d = parseInt(stra,16)
let e = parseInt(strb,16)
let jsResult3 = (d >>> e) & 0xff
let myResult3 = BitwiseLogic.bitwise2arg(d, operator.zeroFillRightShift,e) & 0xff
serial.writeLine("js: " + decToHex(jsResult3) + " myCode: " + decToHex(myResult3))
if (jsResult3 == myResult3) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function checkNot (str: string) {
// convert to number
let a = parseInt(str,16)
// convert to 8-bit unsigned result
let jsResult = ~a & 0xff
let myResult = BitwiseLogic.bitwiseNot(a) & 0xff
serial.writeLine("js: " + decToHex(jsResult) + " myCode: " + decToHex(myResult))
if (jsResult == myResult) {
serial.writeLine("P")
} else {
serial.writeLine("F")
}
}
function decToHex (num: number) {
result = "0x"
nibble = Math.floor(num / 16)
for (let i = 0; i <= 1; i++) {
if (nibble < 10) {
result = "" + result + convertToText(nibble)
} else {
nibble = nibble - 10
result = "" + result + "abcdef".charAt(nibble)
}
nibble = num % 16
}
return result
}
let nibble = 0
let result = ""
serial.writeLine("Testing ~")
checkNot("a5")
checkNot("5a")
serial.writeLine("Testing &")
checkAnd("a5", "0f")
checkAnd("a5", "f0")
serial.writeLine("Testing |")
checkOr("a5", "0f")
checkOr("a5", "f0")
serial.writeLine("Testing ^")
checkXor("a5", "0f")
checkXor("a5", "f0")
serial.writeLine("Testing <<")
checkLeftShift("a5", "1")
checkLeftShift("a5", "4")
serial.writeLine("Testing >>")
checkRightShift("a5", "1")
checkRightShift("a5", "4")
serial.writeLine("Testing >>")
checkZeroFillRightShift("a5", "1")
checkZeroFillRightShift("a5", "4")