-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerics_test.naml
More file actions
180 lines (152 loc) Β· 3.94 KB
/
generics_test.naml
File metadata and controls
180 lines (152 loc) Β· 3.94 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
///
/// Generics Test - Basic generic features
///
enum UserStatus {
Active,
Inactive,
Suspended(string),
PendingVerification(string)
}
enum Result<T, E> {
Ok(T),
Err(E)
}
struct Pair<A, B> {
first: A,
second: B
}
struct Box<T> {
value: T
}
fn test_user_status() {
println("=== UserStatus Enum Tests ===");
var active: UserStatus = UserStatus::Active;
var inactive: UserStatus = UserStatus::Inactive;
var suspended: UserStatus = UserStatus::Suspended("policy violation");
var pending: UserStatus = UserStatus::PendingVerification("email sent");
print("Testing Active: ");
switch (active) {
case Active: {
println("PASS");
}
default: {
println("FAIL");
}
}
print("Testing Inactive: ");
switch (inactive) {
case Inactive: {
println("PASS");
}
default: {
println("FAIL");
}
}
print("Testing Suspended with data: ");
switch (suspended) {
case Suspended(reason): {
print("PASS - reason: ");
println(reason);
}
default: {
println("FAIL");
}
}
print("Testing PendingVerification: ");
switch (pending) {
case PendingVerification(msg): {
print("PASS - msg: ");
println(msg);
}
default: {
println("FAIL");
}
}
print("Testing switch default for Inactive: ");
switch (inactive) {
case Active: {
println("FAIL");
}
case Suspended(r): {
println("FAIL");
}
default: {
println("PASS - matched default");
}
}
}
fn test_generic_struct() {
println("=== Generic Struct Tests ===");
var int_pair: Pair<int, int> = Pair { first: 10, second: 20 };
print("Testing Pair<int, int>.first: ");
if (int_pair.first == 10) {
println("PASS");
} else {
println("FAIL");
}
print("Testing Pair<int, int>.second: ");
if (int_pair.second == 20) {
println("PASS");
} else {
println("FAIL");
}
var mixed_pair: Pair<string, int> = Pair { first: "hello", second: 42 };
print("Testing Pair<string, int>.first: ");
if (mixed_pair.first == "hello") {
println("PASS");
} else {
println("FAIL");
}
print("Testing Pair<string, int>.second: ");
if (mixed_pair.second == 42) {
println("PASS");
} else {
println("FAIL");
}
var nested: Pair<Box<int>, string> = Pair { first: Box { value: 100 }, second: "nested" };
print("Testing nested generic: ");
if (nested.second == "nested") {
println("PASS");
} else {
println("FAIL");
}
}
fn test_generic_box() {
println("=== Generic Box Tests ===");
var int_box: Box<int> = Box { value: 42 };
print("Testing Box<int>.value: ");
if (int_box.value == 42) {
println("PASS");
} else {
println("FAIL");
}
var str_box: Box<string> = Box { value: "boxed string" };
print("Testing Box<string>.value: ");
if (str_box.value == "boxed string") {
println("PASS");
} else {
println("FAIL");
}
var point_box: Box<Pair<int, int>> = Box { value: Pair { first: 1, second: 2 } };
print("Testing Box<Pair>.value.first: ");
if (point_box.value.first == 1) {
println("PASS");
} else {
println("FAIL");
}
}
fn main() {
println("========================================");
println(" NAML Generics Test ");
println("========================================");
println("");
test_user_status();
println("");
test_generic_struct();
println("");
test_generic_box();
println("");
println("========================================");
println(" Tests Completed! ");
println("========================================");
}