Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions vlib/v/gen/wasm/tests/advanced_flow.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Test advanced control flow and function pointers simulation
// This exercises complex conditionals and branching

fn operation_add(a i64, b i64) i64 {
return a + b
}

fn operation_sub(a i64, b i64) i64 {
return a - b
}

fn operation_mul(a i64, b i64) i64 {
return a * b
}

fn operation_div(a i64, b i64) i64 {
if b == 0 {
return 0
}
return a / b
}

// Simulate function dispatch using if-else chain
fn calculate(op int, a i64, b i64) i64 {
if op == 0 {
return operation_add(a, b)
} else if op == 1 {
return operation_sub(a, b)
} else if op == 2 {
return operation_mul(a, b)
} else if op == 3 {
return operation_div(a, b)
} else {
return 0
}
}

// Complex nested control flow
fn categorize_number(n i64) int {
if n < 0 {
if n < -100 {
return -3 // very negative
} else if n < -10 {
return -2 // negative
} else {
return -1 // slightly negative
}
} else if n == 0 {
return 0 // zero
} else {
if n > 100 {
return 3 // very positive
} else if n > 10 {
return 2 // positive
} else {
return 1 // slightly positive
}
}
}

// Test various comparison operations
fn compare_values(a i64, b i64) int {
if a == b {
return 0 // equal
} else if a < b {
return -1 // less
} else if a > b {
return 1 // greater
} else {
return 99 // unknown
}
}

// Nested loops with breaks and continues
fn sum_with_skip(limit int, skip int) i64 {
mut sum := i64(0)
for i := 0; i < limit; i++ {
if i == skip {
continue
}
if i > limit / 2 && i % 2 == 0 {
break
}
sum += i
}
return sum
}

// Multiple return paths
fn classify_range(val i64) int {
if val < 0 {
return -1
}
if val == 0 {
return 0
}
if val < 10 {
return 1
}
if val < 100 {
return 2
}
return 3
}

fn abs_diff(a i64, b i64) i64 {
if a > b {
return a - b
}
return b - a
}

fn main() {
println('=== Advanced Control Flow Tests ===')

println('--- Calculator Dispatch')
println(calculate(0, 10, 5))
println(calculate(1, 10, 5))
println(calculate(2, 10, 5))
println(calculate(3, 10, 5))
println(calculate(99, 10, 5))

println('--- Number Categorization')
println(categorize_number(-150))
println(categorize_number(-50))
println(categorize_number(-5))
println(categorize_number(0))
println(categorize_number(5))
println(categorize_number(50))
println(categorize_number(150))

println('--- Value Comparison')
println(compare_values(10, 10))
println(compare_values(5, 10))
println(compare_values(15, 10))

println('--- Loop with Skip')
println(sum_with_skip(10, 3))
println(sum_with_skip(20, 5))

println('--- Range Classification')
println(classify_range(-5))
println(classify_range(0))
println(classify_range(5))
println(classify_range(50))
println(classify_range(500))

println('--- Absolute Difference')
println(abs_diff(10, 20))
println(abs_diff(20, 10))
println(abs_diff(5, 5))
}
32 changes: 32 additions & 0 deletions vlib/v/gen/wasm/tests/advanced_flow.vv.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== Advanced Control Flow Tests ===
--- Calculator Dispatch
15
5
50
2
0
--- Number Categorization
-3
-2
-1
0
1
2
3
--- Value Comparison
0
-1
1
--- Loop with Skip
6
15
--- Range Classification
-1
0
1
2
3
--- Absolute Difference
10
10
0
Binary file added vlib/v/gen/wasm/tests/advanced_flow.wasm
Binary file not shown.
136 changes: 136 additions & 0 deletions vlib/v/gen/wasm/tests/memory_ops.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Test memory operations and bitwise algorithms
// This tests the wasm memory model and complex data manipulation

fn abs_i64(n i64) i64 {
if n < 0 {
return -n
}
return n
}

fn max_i64(a i64, b i64) i64 {
if a > b {
return a
}
return b
}

fn min_i64(a i64, b i64) i64 {
if a < b {
return a
}
return b
}

fn clamp(val i64, min_val i64, max_val i64) i64 {
if val < min_val {
return min_val
}
if val > max_val {
return max_val
}
return val
}

fn count_bits(n i64) int {
mut count := 0
mut num := n
for num != 0 {
count += int(num & 1)
num >>= 1
}
return count
}

fn reverse_bits_32(n u32) u32 {
mut result := u32(0)
mut val := n
for i := 0; i < 32; i++ {
result <<= 1
result |= val & 1
val >>= 1
}
return result
}

fn is_power_of_two(n i64) bool {
if n <= 0 {
return false
}
return (n & (n - 1)) == 0
}

fn next_power_of_two(n i64) i64 {
if n <= 1 {
return 1
}
mut power := i64(1)
for power < n {
power <<= 1
}
return power
}

fn hamming_distance(a i64, b i64) int {
xor_val := a ^ b
return count_bits(xor_val)
}

fn rotate_left_32(n u32, shift int) u32 {
s := u32(shift) & 31
return (n << s) | (n >> (32 - s))
}

fn rotate_right_32(n u32, shift int) u32 {
s := u32(shift) & 31
return (n >> s) | (n << (32 - s))
}

fn swap_bytes_32(n u32) u32 {
return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) | ((n & 0xFF000000) >> 24)
}

fn sign_extend_8(n u8) i64 {
if n & 0x80 != 0 {
return i64(n) | i64(0xFFFFFFFFFFFFFF00)
}
return i64(n)
}

fn main() {
println('=== Memory and Bit Operation Tests ===')

println('--- Basic Math Operations')
println(abs_i64(-42))
println(abs_i64(42))
println(max_i64(10, 20))
println(min_i64(10, 20))
println(clamp(15, 10, 20))
println(clamp(5, 10, 20))
println(clamp(25, 10, 20))

println('--- Bit Counting Operations')
println(count_bits(15))
println(count_bits(255))
println(count_bits(0))
println(count_bits(1023))

println('--- Power of Two Operations')
println(is_power_of_two(16))
println(is_power_of_two(15))
println(next_power_of_two(10))
println(next_power_of_two(16))

println('--- Hamming Distance')
println(hamming_distance(7, 15))
println(hamming_distance(0, 255))

println('--- Bit Rotation')
println(rotate_left_32(0x12345678, 8))
println(rotate_right_32(0x12345678, 8))

println('--- Byte Operations')
println(swap_bytes_32(0x12345678))
println(sign_extend_8(0x7F))
println(sign_extend_8(0xFF))
}
29 changes: 29 additions & 0 deletions vlib/v/gen/wasm/tests/memory_ops.vv.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== Memory and Bit Operation Tests ===
--- Basic Math Operations
42
42
20
10
15
10
20
--- Bit Counting Operations
4
8
0
10
--- Power of Two Operations
true
false
16
16
--- Hamming Distance
4
8
--- Bit Rotation
305419896
2018915346
--- Byte Operations
2018915346
127
-1
Binary file added vlib/v/gen/wasm/tests/memory_ops.wasm
Binary file not shown.
Loading
Loading