Skip to content

Commit d91a263

Browse files
Copilotenghitalo
andcommitted
Add comprehensive tests for arrays of all types: structs, nested arrays, complex types
Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
1 parent ff598c8 commit d91a263

10 files changed

Lines changed: 245 additions & 0 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test arrays with complex nested structures
2+
struct Inner {
3+
value int
4+
}
5+
6+
struct Outer {
7+
inner Inner
8+
data int
9+
}
10+
11+
fn main() {
12+
// Test array of structs containing structs
13+
mut complex := []Outer{}
14+
complex << Outer{Inner{10}, 100}
15+
complex << Outer{Inner{20}, 200}
16+
complex << Outer{Inner{30}, 300}
17+
18+
println('Complex nested structures:')
19+
println(complex[0].inner.value)
20+
println(complex[0].data)
21+
println(complex[1].inner.value)
22+
println(complex[1].data)
23+
println(complex[2].inner.value)
24+
println(complex[2].data)
25+
26+
// Test 3-level nesting with arrays
27+
mut deep := [][][]int{}
28+
mut level2 := [][]int{}
29+
mut level1 := []int{}
30+
level1 << 42
31+
level2 << level1
32+
deep << level2
33+
34+
println('Deep nesting:')
35+
println(deep[0][0][0])
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Complex nested structures:
2+
10
3+
100
4+
20
5+
200
6+
30
7+
300
8+
Deep nesting:
9+
42
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Test arrays with different element sizes
2+
struct SmallStruct {
3+
a int
4+
}
5+
6+
struct MediumStruct {
7+
a int
8+
b int
9+
c int
10+
}
11+
12+
struct LargeStruct {
13+
a int
14+
b int
15+
c int
16+
d int
17+
e int
18+
f int
19+
}
20+
21+
fn main() {
22+
// Test small struct array
23+
mut small := []SmallStruct{}
24+
small << SmallStruct{10}
25+
small << SmallStruct{20}
26+
println('Small structs:')
27+
println(small[0].a)
28+
println(small[1].a)
29+
30+
// Test medium struct array
31+
mut medium := []MediumStruct{}
32+
medium << MediumStruct{1, 2, 3}
33+
medium << MediumStruct{4, 5, 6}
34+
println('Medium structs:')
35+
println(medium[0].a)
36+
println(medium[0].b)
37+
println(medium[0].c)
38+
println(medium[1].a)
39+
println(medium[1].b)
40+
println(medium[1].c)
41+
42+
// Test large struct array
43+
mut large := []LargeStruct{}
44+
large << LargeStruct{1, 2, 3, 4, 5, 6}
45+
large << LargeStruct{7, 8, 9, 10, 11, 12}
46+
println('Large structs:')
47+
println(large[0].a)
48+
println(large[0].f)
49+
println(large[1].a)
50+
println(large[1].f)
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Small structs:
2+
10
3+
20
4+
Medium structs:
5+
1
6+
2
7+
3
8+
4
9+
5
10+
6
11+
Large structs:
12+
1
13+
6
14+
7
15+
12
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test arrays with capacity growth
2+
fn main() {
3+
// Test growth from empty array
4+
mut nums := []int{}
5+
6+
// Append many elements to trigger multiple capacity doublings
7+
// Starting capacity is typically 0 or small, will grow to 2, 4, 8, 16...
8+
nums << 1
9+
nums << 2
10+
nums << 3
11+
nums << 4
12+
nums << 5
13+
nums << 6
14+
nums << 7
15+
nums << 8
16+
nums << 9
17+
nums << 10
18+
19+
println('Array with growth:')
20+
println(nums[0])
21+
println(nums[4])
22+
println(nums[9])
23+
24+
// Test growth from initialized array
25+
mut values := [100, 200]
26+
values << 300
27+
values << 400
28+
values << 500
29+
30+
println('Initialized array with growth:')
31+
println(values[0])
32+
println(values[1])
33+
println(values[2])
34+
println(values[3])
35+
println(values[4])
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Array with growth:
2+
1
3+
5
4+
10
5+
Initialized array with growth:
6+
100
7+
200
8+
300
9+
400
10+
500
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Test nested arrays (2D arrays)
2+
fn main() {
3+
// Test array of arrays (nested arrays)
4+
mut matrix := [][]int{}
5+
6+
mut row1 := []int{}
7+
row1 << 1
8+
row1 << 2
9+
row1 << 3
10+
11+
mut row2 := []int{}
12+
row2 << 4
13+
row2 << 5
14+
row2 << 6
15+
16+
matrix << row1
17+
matrix << row2
18+
19+
println('Nested array (matrix):')
20+
println(matrix[0][0])
21+
println(matrix[0][1])
22+
println(matrix[0][2])
23+
println(matrix[1][0])
24+
println(matrix[1][1])
25+
println(matrix[1][2])
26+
27+
// Test initialization with nested literals
28+
grid := [[10, 20], [30, 40]]
29+
println('Grid:')
30+
println(grid[0][0])
31+
println(grid[0][1])
32+
println(grid[1][0])
33+
println(grid[1][1])
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Nested array (matrix):
2+
1
3+
2
4+
3
5+
4
6+
5
7+
6
8+
Grid:
9+
10
10+
20
11+
30
12+
40
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Test arrays of structs
2+
struct Point {
3+
x int
4+
y int
5+
}
6+
7+
struct Person {
8+
name string
9+
age int
10+
}
11+
12+
fn main() {
13+
// Test array of simple structs
14+
mut points := []Point{}
15+
points << Point{10, 20}
16+
points << Point{30, 40}
17+
points << Point{50, 60}
18+
19+
println('Points array:')
20+
println(points[0].x)
21+
println(points[0].y)
22+
println(points[1].x)
23+
println(points[1].y)
24+
println(points[2].x)
25+
println(points[2].y)
26+
27+
// Test array initialization with struct literals
28+
people := [Person{'Alice', 25}, Person{'Bob', 30}]
29+
println('People array:')
30+
println(people[0].age)
31+
println(people[1].age)
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Points array:
2+
10
3+
20
4+
30
5+
40
6+
50
7+
60
8+
People array:
9+
25
10+
30

0 commit comments

Comments
 (0)