| 1 | import sync |
| 2 | |
| 3 | fn test_array_of_floats() { |
| 4 | // f64 array |
| 5 | aa := [1.2, 3.4, 5.67] |
| 6 | assert aa.str() == '[1.2, 3.4, 5.67]' |
| 7 | assert '${aa}' == '[1.2, 3.4, 5.67]' |
| 8 | // f32 array |
| 9 | bb := [f32(1.2), 3.4, 5.67] |
| 10 | assert bb.str() == '[1.2, 3.4, 5.67]' |
| 11 | assert '${bb}' == '[1.2, 3.4, 5.67]' |
| 12 | } |
| 13 | |
| 14 | fn test_array_of_bools() { |
| 15 | aa := [true, false, true] |
| 16 | assert aa.str() == '[true, false, true]' |
| 17 | assert '${aa}' == '[true, false, true]' |
| 18 | } |
| 19 | |
| 20 | fn test_array_of_ints() { |
| 21 | // int |
| 22 | a1 := [11, 22, 33] |
| 23 | assert a1.str() == '[11, 22, 33]' |
| 24 | assert '${a1}' == '[11, 22, 33]' |
| 25 | // u32 |
| 26 | a2 := [u32(11), 22, 33] |
| 27 | assert a2.str() == '[11, 22, 33]' |
| 28 | assert '${a2}' == '[11, 22, 33]' |
| 29 | // i16 |
| 30 | b1 := [i16(11), 22, 33] |
| 31 | assert b1.str() == '[11, 22, 33]' |
| 32 | assert '${b1}' == '[11, 22, 33]' |
| 33 | // u16 |
| 34 | b2 := [u16(11), 22, 33] |
| 35 | assert b2.str() == '[11, 22, 33]' |
| 36 | assert '${b2}' == '[11, 22, 33]' |
| 37 | // i64 |
| 38 | c1 := [i64(11), 22, 33] |
| 39 | assert c1.str() == '[11, 22, 33]' |
| 40 | assert '${c1}' == '[11, 22, 33]' |
| 41 | // u64 |
| 42 | c2 := [u64(11), 22, 33] |
| 43 | assert c2.str() == '[11, 22, 33]' |
| 44 | assert '${c2}' == '[11, 22, 33]' |
| 45 | } |
| 46 | |
| 47 | fn test_array_of_runes() { |
| 48 | aa := [`a`, `b`, `c`] |
| 49 | assert aa.str() == '[`a`, `b`, `c`]' |
| 50 | assert '${aa}' == '[`a`, `b`, `c`]' |
| 51 | } |
| 52 | |
| 53 | fn test_array_of_strings() { |
| 54 | aa := ['aa', 'bb', 'cc'] |
| 55 | assert aa.str() == "['aa', 'bb', 'cc']" |
| 56 | assert '${aa}' == "['aa', 'bb', 'cc']" |
| 57 | } |
| 58 | |
| 59 | fn test_map_of_ints() { |
| 60 | aa := { |
| 61 | 'a': 1 |
| 62 | 'b': 2 |
| 63 | 'c': 3 |
| 64 | } |
| 65 | assert aa.str() == "{'a': 1, 'b': 2, 'c': 3}" |
| 66 | assert '${aa}' == "{'a': 1, 'b': 2, 'c': 3}" |
| 67 | } |
| 68 | |
| 69 | fn test_map_of_strings() { |
| 70 | aa := { |
| 71 | 'a': '1' |
| 72 | 'b': '2' |
| 73 | 'c': '3' |
| 74 | } |
| 75 | assert aa.str() == "{'a': '1', 'b': '2', 'c': '3'}" |
| 76 | assert '${aa}' == "{'a': '1', 'b': '2', 'c': '3'}" |
| 77 | } |
| 78 | |
| 79 | fn test_map_of_floats() { |
| 80 | aa := { |
| 81 | 'a': 1.1 |
| 82 | 'b': 2.2 |
| 83 | 'c': 3.3 |
| 84 | } |
| 85 | assert aa.str() == "{'a': 1.1, 'b': 2.2, 'c': 3.3}" |
| 86 | assert '${aa}' == "{'a': 1.1, 'b': 2.2, 'c': 3.3}" |
| 87 | } |
| 88 | |
| 89 | fn test_map_of_runes() { |
| 90 | aa := { |
| 91 | 'a': `a` |
| 92 | 'b': `b` |
| 93 | 'c': `c` |
| 94 | } |
| 95 | assert aa.str() == "{'a': `a`, 'b': `b`, 'c': `c`}" |
| 96 | assert '${aa}' == "{'a': `a`, 'b': `b`, 'c': `c`}" |
| 97 | } |
| 98 | |
| 99 | fn test_map_of_bools() { |
| 100 | aa := { |
| 101 | 'a': true |
| 102 | 'b': false |
| 103 | 'c': true |
| 104 | } |
| 105 | assert aa.str() == "{'a': true, 'b': false, 'c': true}" |
| 106 | assert '${aa}' == "{'a': true, 'b': false, 'c': true}" |
| 107 | } |
| 108 | |
| 109 | fn test_fixed_array_of_floats() { |
| 110 | // f64 array |
| 111 | aa := [1.2, 3.4, 5.67]! |
| 112 | assert aa.str() == '[1.2, 3.4, 5.67]' |
| 113 | assert '${aa}' == '[1.2, 3.4, 5.67]' |
| 114 | // f32 array |
| 115 | bb := [f32(1.2), 3.4, 5.67]! |
| 116 | assert bb.str() == '[1.2, 3.4, 5.67]' |
| 117 | assert '${bb}' == '[1.2, 3.4, 5.67]' |
| 118 | } |
| 119 | |
| 120 | fn test_fixed_array_of_bools() { |
| 121 | aa := [true, false, true]! |
| 122 | assert aa.str() == '[true, false, true]' |
| 123 | assert '${aa}' == '[true, false, true]' |
| 124 | } |
| 125 | |
| 126 | fn test_fixed_array_of_ints() { |
| 127 | // int |
| 128 | a1 := [11, 22, 33]! |
| 129 | assert a1.str() == '[11, 22, 33]' |
| 130 | assert '${a1}' == '[11, 22, 33]' |
| 131 | // u32 |
| 132 | a2 := [u32(11), 22, 33]! |
| 133 | assert a2.str() == '[11, 22, 33]' |
| 134 | assert '${a2}' == '[11, 22, 33]' |
| 135 | // i16 |
| 136 | b1 := [i16(11), 22, 33]! |
| 137 | assert b1.str() == '[11, 22, 33]' |
| 138 | assert '${b1}' == '[11, 22, 33]' |
| 139 | // u16 |
| 140 | b2 := [u16(11), 22, 33]! |
| 141 | assert b2.str() == '[11, 22, 33]' |
| 142 | assert '${b2}' == '[11, 22, 33]' |
| 143 | // i64 |
| 144 | c1 := [i64(11), 22, 33]! |
| 145 | assert c1.str() == '[11, 22, 33]' |
| 146 | assert '${c1}' == '[11, 22, 33]' |
| 147 | // u64 |
| 148 | c2 := [u64(11), 22, 33]! |
| 149 | assert c2.str() == '[11, 22, 33]' |
| 150 | assert '${c2}' == '[11, 22, 33]' |
| 151 | } |
| 152 | |
| 153 | fn test_fixed_array_of_runes() { |
| 154 | aa := [`a`, `b`, `c`]! |
| 155 | assert aa.str() == '[`a`, `b`, `c`]' |
| 156 | assert '${aa}' == '[`a`, `b`, `c`]' |
| 157 | } |
| 158 | |
| 159 | fn test_fixed_array_of_strings() { |
| 160 | aa := ['aa', 'bb', 'cc']! |
| 161 | assert aa.str() == "['aa', 'bb', 'cc']" |
| 162 | assert '${aa}' == "['aa', 'bb', 'cc']" |
| 163 | } |
| 164 | |
| 165 | struct Wrapper { |
| 166 | foo &string |
| 167 | } |
| 168 | |
| 169 | fn test_struct_with_string_pointer() { |
| 170 | s := 'test' |
| 171 | w := Wrapper{&s} |
| 172 | assert '${w}' == "Wrapper{\n foo: &'test'\n}" |
| 173 | assert w.str() == "Wrapper{\n foo: &'test'\n}" |
| 174 | } |
| 175 | |
| 176 | struct Wrapper2 { |
| 177 | foo &int |
| 178 | } |
| 179 | |
| 180 | fn test_struct_with_int_pointer() { |
| 181 | i := 5 |
| 182 | w := Wrapper2{&i} |
| 183 | assert '${w}' == 'Wrapper2{\n foo: &5\n}' |
| 184 | assert w.str() == 'Wrapper2{\n foo: &5\n}' |
| 185 | } |
| 186 | |
| 187 | struct Wrapper3 { |
| 188 | foo &bool |
| 189 | } |
| 190 | |
| 191 | fn test_struct_with_bool_pointer() { |
| 192 | b := true |
| 193 | w := Wrapper3{&b} |
| 194 | assert '${w}' == 'Wrapper3{\n foo: &true\n}' |
| 195 | assert w.str() == 'Wrapper3{\n foo: &true\n}' |
| 196 | } |
| 197 | |
| 198 | struct Foo {} |
| 199 | |
| 200 | struct Wrapper4 { |
| 201 | foo &Foo |
| 202 | } |
| 203 | |
| 204 | fn test_struct_with_struct_pointer() { |
| 205 | b := Foo{} |
| 206 | w := Wrapper4{&b} |
| 207 | assert '${w}' == 'Wrapper4{\n foo: &Foo{}\n}' |
| 208 | assert w.str() == 'Wrapper4{\n foo: &Foo{}\n}' |
| 209 | } |
| 210 | |
| 211 | fn test_struct_with_nil() { |
| 212 | w := Wrapper4{unsafe { nil }} |
| 213 | assert '${w}' == 'Wrapper4{\n foo: &nil\n}' |
| 214 | assert w.str() == 'Wrapper4{\n foo: &nil\n}' |
| 215 | } |
| 216 | |
| 217 | struct Wrapper5 { |
| 218 | foo &f32 |
| 219 | } |
| 220 | |
| 221 | fn test_struct_with_f32_pointer() { |
| 222 | i := f32(5.1) |
| 223 | w := Wrapper5{&i} |
| 224 | assert '${w}' == 'Wrapper5{\n foo: &5.1\n}' |
| 225 | assert w.str() == 'Wrapper5{\n foo: &5.1\n}' |
| 226 | } |
| 227 | |
| 228 | struct TestStruct { |
| 229 | x int |
| 230 | } |
| 231 | |
| 232 | struct ArrayWithStruct { |
| 233 | foo []TestStruct |
| 234 | } |
| 235 | |
| 236 | fn test_array_with_struct() { |
| 237 | a := ArrayWithStruct{[TestStruct{}]} |
| 238 | assert a.str() == 'ArrayWithStruct{\n foo: [TestStruct{\n x: 0\n }]\n}' |
| 239 | assert '${a}' == 'ArrayWithStruct{\n foo: [TestStruct{\n x: 0\n }]\n}' |
| 240 | } |
| 241 | |
| 242 | struct MapWithStruct { |
| 243 | foo map[string]TestStruct |
| 244 | } |
| 245 | |
| 246 | fn test_map_with_struct() { |
| 247 | a := MapWithStruct{{ |
| 248 | 'test': TestStruct{} |
| 249 | }} |
| 250 | assert a.str() == "MapWithStruct{\n foo: {'test': TestStruct{\n x: 0\n }}\n}" |
| 251 | assert '${a}' == "MapWithStruct{\n foo: {'test': TestStruct{\n x: 0\n }}\n}" |
| 252 | } |
| 253 | |
| 254 | struct ForGeneric {} |
| 255 | |
| 256 | fn generic_fn_interpolation[T](p T) string { |
| 257 | return '${p}' |
| 258 | } |
| 259 | |
| 260 | fn generic_fn_str[T](p T) string { |
| 261 | return p.str() |
| 262 | } |
| 263 | |
| 264 | fn generic_fn_println[T](p T) string { |
| 265 | println(p) |
| 266 | return typeof(p).name |
| 267 | } |
| 268 | |
| 269 | fn test_generic_auto_str() { |
| 270 | s := ForGeneric{} |
| 271 | assert generic_fn_interpolation(s) == 'ForGeneric{}' |
| 272 | assert generic_fn_str(s) == 'ForGeneric{}' |
| 273 | } |
| 274 | |
| 275 | fn test_generic_pointer_auto_str_for_println() { |
| 276 | s := ForGeneric{} |
| 277 | assert generic_fn_println(&s) == '&ForGeneric' |
| 278 | p1 := &s |
| 279 | p2 := &p1 |
| 280 | p3 := &p2 |
| 281 | assert generic_fn_println(p3) == '&&&ForGeneric' |
| 282 | } |
| 283 | |
| 284 | type Alias1 = int |
| 285 | |
| 286 | fn test_alias_in_array() { |
| 287 | t := [Alias1(1)] |
| 288 | assert t.str() == '[1]' |
| 289 | assert '${t}' == '[1]' |
| 290 | } |
| 291 | |
| 292 | type Alias2 = int |
| 293 | |
| 294 | fn test_alias_in_fixed_array() { |
| 295 | t := [Alias1(1)]! |
| 296 | assert t.str() == '[1]' |
| 297 | assert '${t}' == '[1]' |
| 298 | } |
| 299 | |
| 300 | fn test_alias_int() { |
| 301 | a := Alias1(1) |
| 302 | assert a.str() == '1' |
| 303 | assert '${a}' == '1' |
| 304 | } |
| 305 | |
| 306 | type Alias3 = string |
| 307 | |
| 308 | fn test_alias_string() { |
| 309 | s := 'test' |
| 310 | a := Alias3(s) |
| 311 | assert a.str() == s |
| 312 | assert '${a}' == s |
| 313 | } |
| 314 | |
| 315 | type TestAlias = TestStruct |
| 316 | |
| 317 | fn test_alias_struct() { |
| 318 | ts := TestStruct{} |
| 319 | t := TestAlias(ts) |
| 320 | assert t.str() == 'TestAlias(${ts})' |
| 321 | assert '${t}' == 'TestAlias(TestStruct{\n x: 0\n})' |
| 322 | } |
| 323 | |
| 324 | struct GenericStruct[T] { |
| 325 | x T |
| 326 | } |
| 327 | |
| 328 | fn test_generic_struct() { |
| 329 | x := GenericStruct[TestStruct]{} |
| 330 | assert '${x}' == 'GenericStruct[TestStruct]{\n x: TestStruct{\n x: 0\n }\n}' |
| 331 | assert x.str() == 'GenericStruct[TestStruct]{\n x: TestStruct{\n x: 0\n }\n}' |
| 332 | } |
| 333 | |
| 334 | struct MultiGenericStruct[T, X] { |
| 335 | t T |
| 336 | x X |
| 337 | } |
| 338 | |
| 339 | fn test_multi_generic_struct() { |
| 340 | x := MultiGenericStruct[TestStruct, TestStruct]{} |
| 341 | assert '${x}' == 'MultiGenericStruct[TestStruct, TestStruct]{\n t: TestStruct{\n x: 0\n }\n x: TestStruct{\n x: 0\n }\n}' |
| 342 | assert x.str() == 'MultiGenericStruct[TestStruct, TestStruct]{\n t: TestStruct{\n x: 0\n }\n x: TestStruct{\n x: 0\n }\n}' |
| 343 | } |
| 344 | |
| 345 | fn create_option_err() !string { |
| 346 | return error('this is an error') |
| 347 | } |
| 348 | |
| 349 | fn test_result_err() { |
| 350 | assert '${create_option_err() or { 'Result(error: this is an error)' }}' == 'Result(error: this is an error)' |
| 351 | } |
| 352 | |
| 353 | fn create_option_none() ?string { |
| 354 | return none |
| 355 | } |
| 356 | |
| 357 | fn test_option_none() { |
| 358 | assert '${create_option_none()}' == 'Option(none)' |
| 359 | } |
| 360 | |
| 361 | fn create_option_string() ?string { |
| 362 | return 'this is a string' |
| 363 | } |
| 364 | |
| 365 | fn test_option_string() { |
| 366 | assert '${create_option_string()}' == "Option('this is a string')" |
| 367 | } |
| 368 | |
| 369 | fn create_option_int() ?int { |
| 370 | return 5 |
| 371 | } |
| 372 | |
| 373 | fn test_option_int() { |
| 374 | assert '${create_option_int()}' == 'Option(5)' |
| 375 | } |
| 376 | |
| 377 | fn create_option_array() ?[]int { |
| 378 | return [1, 2, 3] |
| 379 | } |
| 380 | |
| 381 | fn test_option_array() { |
| 382 | assert '${create_option_array()}' == 'Option([1, 2, 3])' |
| 383 | } |
| 384 | |
| 385 | fn create_option_struct() ?TestStruct { |
| 386 | return TestStruct{} |
| 387 | } |
| 388 | |
| 389 | fn test_option_struct() { |
| 390 | assert '${create_option_struct()}' == 'Option(TestStruct{\n x: 0\n})' |
| 391 | } |
| 392 | |
| 393 | // struct OptionWrapper { |
| 394 | // x ?TestStruct |
| 395 | // } |
| 396 | |
| 397 | // fn test_struct_with_option() { |
| 398 | // w := OptionWrapper{} |
| 399 | // assert '${w}' == 'OptionWrapper{\n x: Option(error: \'\')\n}' |
| 400 | // } |
| 401 | |
| 402 | /* |
| 403 | TODO: doesn't work yet |
| 404 | struct OptionWrapperInt { |
| 405 | x ?int |
| 406 | } |
| 407 | |
| 408 | fn test_struct_with_option() { |
| 409 | w := OptionWrapperInt{} |
| 410 | assert '${w}' == 'OptionWrapperInt{\n x: Option(error: \'\')\n}' |
| 411 | } |
| 412 | */ |
| 413 | |
| 414 | struct One { |
| 415 | value string = 'one' |
| 416 | } |
| 417 | |
| 418 | struct Two { |
| 419 | value string = 'two' |
| 420 | } |
| 421 | |
| 422 | fn mr_int_int() (int, int) { |
| 423 | return 111, 222 |
| 424 | } |
| 425 | |
| 426 | fn mr_one_two() (One, Two) { |
| 427 | one := One{} |
| 428 | two := Two{} |
| 429 | return one, two |
| 430 | } |
| 431 | |
| 432 | fn mr_fn_fn() (fn (int), fn (int)) { |
| 433 | a := fn (a int) {} |
| 434 | b := fn (a int) {} |
| 435 | return a, b |
| 436 | } |
| 437 | |
| 438 | fn test_multi_return() { |
| 439 | assert '${mr_int_int()}' == '(111, 222)' |
| 440 | assert '${mr_fn_fn()}' == '(fn (int), fn (int))' |
| 441 | assert '${mr_one_two()}' == "(One{ |
| 442 | value: 'one' |
| 443 | }, Two{ |
| 444 | value: 'two' |
| 445 | })" |
| 446 | anon_a := fn () (One, Two) { |
| 447 | one := One{} |
| 448 | two := Two{} |
| 449 | return one, two |
| 450 | } |
| 451 | assert '${anon_a()}' == "(One{ |
| 452 | value: 'one' |
| 453 | }, Two{ |
| 454 | value: 'two' |
| 455 | })" |
| 456 | } |
| 457 | |
| 458 | fn test_fixed_array_of_function() { |
| 459 | a := [println, println]! |
| 460 | assert '${a}' == '[fn (string), fn (string)]' |
| 461 | } |
| 462 | |
| 463 | struct CTypeDefStruct { |
| 464 | mutex &sync.Mutex = sync.new_mutex() |
| 465 | } |
| 466 | |
| 467 | fn test_c_struct_typedef() { |
| 468 | c := CTypeDefStruct{} |
| 469 | cstr := c.str() |
| 470 | assert cstr.starts_with('CTypeDefStruct') |
| 471 | assert cstr.contains('mutex: &Mutex(') |
| 472 | } |
| 473 | |
| 474 | struct CharptrToStr { |
| 475 | cp charptr = charptr(0) |
| 476 | } |
| 477 | |
| 478 | fn test_charptr_nil_to_str() { |
| 479 | c := CharptrToStr{} |
| 480 | assert c.str() == r'CharptrToStr{ |
| 481 | cp: C"" |
| 482 | }' |
| 483 | } |
| 484 | |