Skip to content

Commit 34dc733

Browse files
Copilotenghitalo
andcommitted
Add real-world WASM compilation tests
Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
1 parent 3a3afc0 commit 34dc733

9 files changed

Lines changed: 496 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Test advanced control flow and function pointers simulation
2+
// This exercises complex conditionals and branching
3+
4+
fn operation_add(a i64, b i64) i64 {
5+
return a + b
6+
}
7+
8+
fn operation_sub(a i64, b i64) i64 {
9+
return a - b
10+
}
11+
12+
fn operation_mul(a i64, b i64) i64 {
13+
return a * b
14+
}
15+
16+
fn operation_div(a i64, b i64) i64 {
17+
if b == 0 {
18+
return 0
19+
}
20+
return a / b
21+
}
22+
23+
// Simulate function dispatch using if-else chain
24+
fn calculate(op int, a i64, b i64) i64 {
25+
if op == 0 {
26+
return operation_add(a, b)
27+
} else if op == 1 {
28+
return operation_sub(a, b)
29+
} else if op == 2 {
30+
return operation_mul(a, b)
31+
} else if op == 3 {
32+
return operation_div(a, b)
33+
} else {
34+
return 0
35+
}
36+
}
37+
38+
// Complex nested control flow
39+
fn categorize_number(n i64) int {
40+
if n < 0 {
41+
if n < -100 {
42+
return -3 // very negative
43+
} else if n < -10 {
44+
return -2 // negative
45+
} else {
46+
return -1 // slightly negative
47+
}
48+
} else if n == 0 {
49+
return 0 // zero
50+
} else {
51+
if n > 100 {
52+
return 3 // very positive
53+
} else if n > 10 {
54+
return 2 // positive
55+
} else {
56+
return 1 // slightly positive
57+
}
58+
}
59+
}
60+
61+
// Test various comparison operations
62+
fn compare_values(a i64, b i64) int {
63+
if a == b {
64+
return 0 // equal
65+
} else if a < b {
66+
return -1 // less
67+
} else if a > b {
68+
return 1 // greater
69+
} else {
70+
return 99 // unknown
71+
}
72+
}
73+
74+
// Nested loops with breaks and continues
75+
fn sum_with_skip(limit int, skip int) i64 {
76+
mut sum := i64(0)
77+
for i := 0; i < limit; i++ {
78+
if i == skip {
79+
continue
80+
}
81+
if i > limit / 2 && i % 2 == 0 {
82+
break
83+
}
84+
sum += i
85+
}
86+
return sum
87+
}
88+
89+
// Multiple return paths
90+
fn classify_range(val i64) int {
91+
if val < 0 {
92+
return -1
93+
}
94+
if val == 0 {
95+
return 0
96+
}
97+
if val < 10 {
98+
return 1
99+
}
100+
if val < 100 {
101+
return 2
102+
}
103+
return 3
104+
}
105+
106+
fn abs_diff(a i64, b i64) i64 {
107+
if a > b {
108+
return a - b
109+
}
110+
return b - a
111+
}
112+
113+
fn main() {
114+
println('=== Advanced Control Flow Tests ===')
115+
116+
println('--- Calculator Dispatch')
117+
println(calculate(0, 10, 5))
118+
println(calculate(1, 10, 5))
119+
println(calculate(2, 10, 5))
120+
println(calculate(3, 10, 5))
121+
println(calculate(99, 10, 5))
122+
123+
println('--- Number Categorization')
124+
println(categorize_number(-150))
125+
println(categorize_number(-50))
126+
println(categorize_number(-5))
127+
println(categorize_number(0))
128+
println(categorize_number(5))
129+
println(categorize_number(50))
130+
println(categorize_number(150))
131+
132+
println('--- Value Comparison')
133+
println(compare_values(10, 10))
134+
println(compare_values(5, 10))
135+
println(compare_values(15, 10))
136+
137+
println('--- Loop with Skip')
138+
println(sum_with_skip(10, 3))
139+
println(sum_with_skip(20, 5))
140+
141+
println('--- Range Classification')
142+
println(classify_range(-5))
143+
println(classify_range(0))
144+
println(classify_range(5))
145+
println(classify_range(50))
146+
println(classify_range(500))
147+
148+
println('--- Absolute Difference')
149+
println(abs_diff(10, 20))
150+
println(abs_diff(20, 10))
151+
println(abs_diff(5, 5))
152+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== Advanced Control Flow Tests ===
2+
--- Calculator Dispatch
3+
15
4+
5
5+
50
6+
2
7+
0
8+
--- Number Categorization
9+
-3
10+
-2
11+
-1
12+
0
13+
1
14+
2
15+
3
16+
--- Value Comparison
17+
0
18+
-1
19+
1
20+
--- Loop with Skip
21+
6
22+
15
23+
--- Range Classification
24+
-1
25+
0
26+
1
27+
2
28+
3
29+
--- Absolute Difference
30+
10
31+
10
32+
0
3.96 KB
Binary file not shown.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Test memory operations and bitwise algorithms
2+
// This tests the wasm memory model and complex data manipulation
3+
4+
fn abs_i64(n i64) i64 {
5+
if n < 0 {
6+
return -n
7+
}
8+
return n
9+
}
10+
11+
fn max_i64(a i64, b i64) i64 {
12+
if a > b {
13+
return a
14+
}
15+
return b
16+
}
17+
18+
fn min_i64(a i64, b i64) i64 {
19+
if a < b {
20+
return a
21+
}
22+
return b
23+
}
24+
25+
fn clamp(val i64, min_val i64, max_val i64) i64 {
26+
if val < min_val {
27+
return min_val
28+
}
29+
if val > max_val {
30+
return max_val
31+
}
32+
return val
33+
}
34+
35+
fn count_bits(n i64) int {
36+
mut count := 0
37+
mut num := n
38+
for num != 0 {
39+
count += int(num & 1)
40+
num >>= 1
41+
}
42+
return count
43+
}
44+
45+
fn reverse_bits_32(n u32) u32 {
46+
mut result := u32(0)
47+
mut val := n
48+
for i := 0; i < 32; i++ {
49+
result <<= 1
50+
result |= val & 1
51+
val >>= 1
52+
}
53+
return result
54+
}
55+
56+
fn is_power_of_two(n i64) bool {
57+
if n <= 0 {
58+
return false
59+
}
60+
return (n & (n - 1)) == 0
61+
}
62+
63+
fn next_power_of_two(n i64) i64 {
64+
if n <= 1 {
65+
return 1
66+
}
67+
mut power := i64(1)
68+
for power < n {
69+
power <<= 1
70+
}
71+
return power
72+
}
73+
74+
fn hamming_distance(a i64, b i64) int {
75+
xor_val := a ^ b
76+
return count_bits(xor_val)
77+
}
78+
79+
fn rotate_left_32(n u32, shift int) u32 {
80+
s := u32(shift) & 31
81+
return (n << s) | (n >> (32 - s))
82+
}
83+
84+
fn rotate_right_32(n u32, shift int) u32 {
85+
s := u32(shift) & 31
86+
return (n >> s) | (n << (32 - s))
87+
}
88+
89+
fn swap_bytes_32(n u32) u32 {
90+
return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) | ((n & 0xFF000000) >> 24)
91+
}
92+
93+
fn sign_extend_8(n u8) i64 {
94+
if n & 0x80 != 0 {
95+
return i64(n) | i64(0xFFFFFFFFFFFFFF00)
96+
}
97+
return i64(n)
98+
}
99+
100+
fn main() {
101+
println('=== Memory and Bit Operation Tests ===')
102+
103+
println('--- Basic Math Operations')
104+
println(abs_i64(-42))
105+
println(abs_i64(42))
106+
println(max_i64(10, 20))
107+
println(min_i64(10, 20))
108+
println(clamp(15, 10, 20))
109+
println(clamp(5, 10, 20))
110+
println(clamp(25, 10, 20))
111+
112+
println('--- Bit Counting Operations')
113+
println(count_bits(15))
114+
println(count_bits(255))
115+
println(count_bits(0))
116+
println(count_bits(1023))
117+
118+
println('--- Power of Two Operations')
119+
println(is_power_of_two(16))
120+
println(is_power_of_two(15))
121+
println(next_power_of_two(10))
122+
println(next_power_of_two(16))
123+
124+
println('--- Hamming Distance')
125+
println(hamming_distance(7, 15))
126+
println(hamming_distance(0, 255))
127+
128+
println('--- Bit Rotation')
129+
println(rotate_left_32(0x12345678, 8))
130+
println(rotate_right_32(0x12345678, 8))
131+
132+
println('--- Byte Operations')
133+
println(swap_bytes_32(0x12345678))
134+
println(sign_extend_8(0x7F))
135+
println(sign_extend_8(0xFF))
136+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== Memory and Bit Operation Tests ===
2+
--- Basic Math Operations
3+
42
4+
42
5+
20
6+
10
7+
15
8+
10
9+
20
10+
--- Bit Counting Operations
11+
4
12+
8
13+
0
14+
10
15+
--- Power of Two Operations
16+
true
17+
false
18+
16
19+
16
20+
--- Hamming Distance
21+
4
22+
8
23+
--- Bit Rotation
24+
305419896
25+
2018915346
26+
--- Byte Operations
27+
2018915346
28+
127
29+
-1
3.95 KB
Binary file not shown.

0 commit comments

Comments
 (0)