| 1 | // vtest retry: 3 |
| 2 | // vtest build: present_node? |
| 3 | |
| 4 | // import strings |
| 5 | |
| 6 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 7 | // Use of this source code is governed by an MIT license |
| 8 | // that can be found in the LICENSE file. |
| 9 | |
| 10 | struct Foo { |
| 11 | bar int |
| 12 | mut: |
| 13 | str string |
| 14 | } |
| 15 | |
| 16 | fn test_add() { |
| 17 | mut a := 'a' |
| 18 | a += 'b' |
| 19 | assert a == ('ab') |
| 20 | a = 'a' |
| 21 | for i := 1; i < 1000; i++ { |
| 22 | a += 'b' |
| 23 | } |
| 24 | assert a.len == 1000 |
| 25 | assert a.ends_with('bbbbb') |
| 26 | a += '123' |
| 27 | assert a.ends_with('3') |
| 28 | } |
| 29 | |
| 30 | fn test_ends_with() { |
| 31 | a := 'browser.v' |
| 32 | assert a.ends_with('.v') |
| 33 | |
| 34 | s := 'V Programming Language' |
| 35 | assert s.ends_with('guage') == true |
| 36 | assert s.ends_with('Language') == true |
| 37 | assert s.ends_with('Programming Language') == true |
| 38 | assert s.ends_with('V') == false |
| 39 | } |
| 40 | |
| 41 | fn test_between() { |
| 42 | s := 'hello [man] how you doing' |
| 43 | assert s.find_between('[', ']') == 'man' |
| 44 | } |
| 45 | |
| 46 | fn test_compare() { |
| 47 | a := 'Music' |
| 48 | b := 'src' |
| 49 | assert b >= a |
| 50 | } |
| 51 | |
| 52 | fn test_lt() { |
| 53 | a := '' |
| 54 | b := 'a' |
| 55 | c := 'a' |
| 56 | d := 'b' |
| 57 | e := 'aa' |
| 58 | f := 'ab' |
| 59 | assert a < b |
| 60 | assert !(b < c) |
| 61 | assert c < d |
| 62 | assert !(d < e) |
| 63 | assert c < e |
| 64 | assert e < f |
| 65 | } |
| 66 | |
| 67 | fn test_ge() { |
| 68 | a := 'aa' |
| 69 | b := 'aa' |
| 70 | c := 'ab' |
| 71 | d := 'abc' |
| 72 | e := 'aaa' |
| 73 | assert b >= a |
| 74 | assert c >= b |
| 75 | assert d >= c |
| 76 | assert !(c >= d) |
| 77 | assert e >= a |
| 78 | } |
| 79 | |
| 80 | fn test_compare_strings() { |
| 81 | a := 'aa' |
| 82 | b := 'aa' |
| 83 | c := 'ab' |
| 84 | d := 'abc' |
| 85 | e := 'aaa' |
| 86 | assert compare_strings(a, b) == 0 |
| 87 | assert compare_strings(b, c) == -1 |
| 88 | assert compare_strings(c, d) == -1 |
| 89 | assert compare_strings(d, e) == 1 |
| 90 | assert compare_strings(a, e) == -1 |
| 91 | assert compare_strings(e, a) == 1 |
| 92 | } |
| 93 | |
| 94 | fn test_sort() { |
| 95 | mut vals := [ |
| 96 | 'arr', |
| 97 | 'an', |
| 98 | 'a', |
| 99 | 'any', |
| 100 | ] |
| 101 | len := vals.len |
| 102 | vals.sort() |
| 103 | assert len == vals.len |
| 104 | assert vals[0] == 'a' |
| 105 | assert vals[1] == 'an' |
| 106 | assert vals[2] == 'any' |
| 107 | assert vals[3] == 'arr' |
| 108 | } |
| 109 | |
| 110 | fn test_sort_reverse() { |
| 111 | mut vals := [ |
| 112 | 'arr', |
| 113 | 'an', |
| 114 | 'a', |
| 115 | 'any', |
| 116 | ] |
| 117 | len := vals.len |
| 118 | vals.sort(b > a) |
| 119 | assert len == vals.len |
| 120 | assert vals[0] == 'a' |
| 121 | assert vals[1] == 'an' |
| 122 | assert vals[2] == 'any' |
| 123 | assert vals[3] == 'arr' |
| 124 | } |
| 125 | |
| 126 | fn test_split_nth() { |
| 127 | a := '1,2,3' |
| 128 | assert a.split(',').len == 3 |
| 129 | assert a.split_nth(',', -1).len == 3 |
| 130 | assert a.split_nth(',', 0).len == 3 |
| 131 | assert a.split_nth(',', 1).len == 1 |
| 132 | assert a.split_nth(',', 2).len == 2 |
| 133 | assert a.split_nth(',', 10).len == 3 |
| 134 | b := '1::2::3' |
| 135 | assert b.split('::').len == 3 |
| 136 | assert b.split_nth('::', -1).len == 3 |
| 137 | assert b.split_nth('::', 0).len == 3 |
| 138 | assert b.split_nth('::', 1).len == 1 |
| 139 | assert b.split_nth('::', 2).len == 2 |
| 140 | assert b.split_nth('::', 10).len == 3 |
| 141 | c := 'ABCDEF' |
| 142 | println(c.split('').len) |
| 143 | assert c.split('').len == 6 |
| 144 | assert c.split_nth('', 3).len == 3 |
| 145 | assert c.split_nth('BC', -1).len == 2 |
| 146 | d := ',' |
| 147 | assert d.split(',').len == 2 |
| 148 | assert d.split_nth('', 3).len == 1 |
| 149 | assert d.split_nth(',', -1).len == 2 |
| 150 | assert d.split_nth(',', 3).len == 2 |
| 151 | e := ',,,0,,,,,a,,b,' |
| 152 | assert e.split(',,').len == 5 |
| 153 | assert e.split_nth(',,', 3).len == 3 |
| 154 | assert e.split_nth(',', -1).len == 12 |
| 155 | assert e.split_nth(',', 3).len == 3 |
| 156 | } |
| 157 | |
| 158 | fn test_split_nth_values() { |
| 159 | line := 'CMD=eprintln(phase=1)' |
| 160 | |
| 161 | a0 := line.split_nth('=', 0) |
| 162 | assert a0.len == 3 |
| 163 | assert a0[0] == 'CMD' |
| 164 | assert a0[1] == 'eprintln(phase' |
| 165 | assert a0[2] == '1)' |
| 166 | |
| 167 | a1 := line.split_nth('=', 1) |
| 168 | assert a1.len == 1 |
| 169 | assert a1[0] == 'CMD=eprintln(phase=1)' |
| 170 | |
| 171 | a2 := line.split_nth('=', 2) |
| 172 | assert a2.len == 2 |
| 173 | assert a2[0] == 'CMD' |
| 174 | assert a2[1] == 'eprintln(phase=1)' |
| 175 | |
| 176 | a3 := line.split_nth('=', 3) |
| 177 | assert a3.len == 3 |
| 178 | assert a3[0] == 'CMD' |
| 179 | assert a3[1] == 'eprintln(phase' |
| 180 | assert a3[2] == '1)' |
| 181 | |
| 182 | a4 := line.split_nth('=', 4) |
| 183 | assert a4.len == 3 |
| 184 | assert a4[0] == 'CMD' |
| 185 | assert a4[1] == 'eprintln(phase' |
| 186 | assert a4[2] == '1)' |
| 187 | } |
| 188 | |
| 189 | fn test_split() { |
| 190 | mut s := 'volt/twitch.v:34' |
| 191 | mut vals := s.split(':') |
| 192 | assert vals.len == 2 |
| 193 | assert vals[0] == 'volt/twitch.v' |
| 194 | assert vals[1] == '34' |
| 195 | // ///////// |
| 196 | s = '2018-01-01z13:01:02' |
| 197 | vals = s.split('z') |
| 198 | assert vals.len == 2 |
| 199 | assert vals[0] == '2018-01-01' |
| 200 | assert vals[1] == '13:01:02' |
| 201 | // ////////// |
| 202 | s = '4627a862c3dec29fb3182a06b8965e0025759e18___1530207969___blue' |
| 203 | vals = s.split('___') |
| 204 | assert vals.len == 3 |
| 205 | assert vals[0] == '4627a862c3dec29fb3182a06b8965e0025759e18' |
| 206 | assert vals[1] == '1530207969' |
| 207 | assert vals[2] == 'blue' |
| 208 | // ///////// |
| 209 | s = 'lalala' |
| 210 | vals = s.split('a') |
| 211 | assert vals.len == 4 |
| 212 | assert vals[0] == 'l' |
| 213 | assert vals[1] == 'l' |
| 214 | assert vals[2] == 'l' |
| 215 | assert vals[3] == '' |
| 216 | // ///////// |
| 217 | s = 'awesome' |
| 218 | a := s.split('') |
| 219 | assert a.len == 7 |
| 220 | assert a[0] == 'a' |
| 221 | assert a[1] == 'w' |
| 222 | assert a[2] == 'e' |
| 223 | assert a[3] == 's' |
| 224 | assert a[4] == 'o' |
| 225 | assert a[5] == 'm' |
| 226 | assert a[6] == 'e' |
| 227 | // ///////// |
| 228 | s = 'wavy turquoise bags' |
| 229 | vals = s.split(' bags') |
| 230 | assert vals.len == 2 |
| 231 | assert vals[0] == 'wavy turquoise' |
| 232 | assert vals[1] == '' |
| 233 | } |
| 234 | |
| 235 | fn test_split_any() { |
| 236 | mut s := 'aaa' |
| 237 | mut a := s.split_any('') |
| 238 | assert a.len == 3 |
| 239 | assert a[0] == 'a' |
| 240 | assert a[1] == 'a' |
| 241 | assert a[2] == 'a' |
| 242 | s = '' |
| 243 | a = s.split_any('') |
| 244 | assert a.len == 0 |
| 245 | s = '12131415' |
| 246 | a = s.split_any('1') |
| 247 | assert a.len == 5 |
| 248 | assert a[0] == '' |
| 249 | assert a[1] == '2' |
| 250 | assert a[2] == '3' |
| 251 | assert a[3] == '4' |
| 252 | assert a[4] == '5' |
| 253 | s = '12131415' |
| 254 | a = s.split_any('2345') |
| 255 | assert a.len == 4 |
| 256 | assert a[0] == '1' |
| 257 | assert a[1] == '1' |
| 258 | assert a[2] == '1' |
| 259 | assert a[3] == '1' |
| 260 | s = 'a,b,c' |
| 261 | a = s.split_any('],') |
| 262 | assert a.len == 3 |
| 263 | assert a[0] == 'a' |
| 264 | assert a[1] == 'b' |
| 265 | assert a[2] == 'c' |
| 266 | s = 'a]b]c' |
| 267 | a = s.split_any('],') |
| 268 | assert a.len == 3 |
| 269 | assert a[0] == 'a' |
| 270 | assert a[1] == 'b' |
| 271 | assert a[2] == 'c' |
| 272 | s = 'a]b]c' |
| 273 | a = s.split_any('],\\') |
| 274 | assert a.len == 3 |
| 275 | assert a[0] == 'a' |
| 276 | assert a[1] == 'b' |
| 277 | assert a[2] == 'c' |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | fn test_trim_space() { |
| 282 | a := ' a ' |
| 283 | assert a.trim_space() == 'a' |
| 284 | code := ' |
| 285 | |
| 286 | fn main() { |
| 287 | println(2) |
| 288 | } |
| 289 | |
| 290 | ' |
| 291 | code_clean := 'fn main() { |
| 292 | println(2) |
| 293 | }' |
| 294 | assert code.trim_space() == code_clean |
| 295 | }*/ |
| 296 | |
| 297 | /* |
| 298 | fn test_join() { |
| 299 | mut strings := ['a', 'b', 'c'] |
| 300 | mut s := strings.join(' ') |
| 301 | assert s == 'a b c' |
| 302 | strings = [ |
| 303 | 'one |
| 304 | two ', |
| 305 | 'three! |
| 306 | four!', |
| 307 | ] |
| 308 | s = strings.join(' ') |
| 309 | assert s.contains('one') && s.contains('two ') && s.contains('four') |
| 310 | empty := []string{len: 0} |
| 311 | assert empty.join('A') == '' |
| 312 | }*/ |
| 313 | |
| 314 | fn test_clone() { |
| 315 | mut a := 'a' |
| 316 | a += 'a' |
| 317 | a += 'a' |
| 318 | b := a |
| 319 | c := a.clone() |
| 320 | assert c == a |
| 321 | assert c == 'aaa' |
| 322 | assert b == 'aaa' |
| 323 | } |
| 324 | |
| 325 | fn test_replace() { |
| 326 | a := 'hello man!' |
| 327 | mut b := a.replace('man', 'world') |
| 328 | assert b == ('hello world!') |
| 329 | b = b.replace('!', '') |
| 330 | assert b == ('hello world') |
| 331 | b = b.replace('h', 'H') |
| 332 | assert b == ('Hello world') |
| 333 | b = b.replace('foo', 'bar') |
| 334 | assert b == ('Hello world') |
| 335 | s := 'hey man how are you' |
| 336 | assert s.replace('man ', '') == 'hey how are you' |
| 337 | lol := 'lol lol lol' |
| 338 | assert lol.replace('lol', 'LOL') == 'LOL LOL LOL' |
| 339 | b = 'oneBtwoBBthree' |
| 340 | assert b.replace('B', '') == 'onetwothree' |
| 341 | b = '*charptr' |
| 342 | assert b.replace('charptr', 'byteptr') == '*byteptr' |
| 343 | c := 'abc' |
| 344 | println(c.replace('', '-')) |
| 345 | // assert c.replace('', '-') == c |
| 346 | v := 'a b c d' |
| 347 | assert v.replace(' ', ' ') == 'a b c d' |
| 348 | } |
| 349 | |
| 350 | fn test_replace_each() { |
| 351 | s := 'hello man man :)' |
| 352 | q := s.replace_each([ |
| 353 | 'man', |
| 354 | 'dude', |
| 355 | 'hello', |
| 356 | 'hey', |
| 357 | ]) |
| 358 | assert q == 'hey dude dude :)' |
| 359 | bb := '[b]bold[/b] [code]code[/code]' |
| 360 | assert bb.replace_each([ |
| 361 | '[b]', |
| 362 | '<b>', |
| 363 | '[/b]', |
| 364 | '</b>', |
| 365 | '[code]', |
| 366 | '<code>', |
| 367 | '[/code]', |
| 368 | '</code>', |
| 369 | ]) == '<b>bold</b> <code>code</code>' |
| 370 | bb2 := '[b]cool[/b]' |
| 371 | assert bb2.replace_each([ |
| 372 | '[b]', |
| 373 | '<b>', |
| 374 | '[/b]', |
| 375 | '</b>', |
| 376 | ]) == '<b>cool</b>' |
| 377 | t := 'aaaaaaaa' |
| 378 | y := t.replace_each([ |
| 379 | 'aa', |
| 380 | 'b', |
| 381 | ]) |
| 382 | assert y == 'bbbb' |
| 383 | s2 := 'hello_world hello' |
| 384 | assert s2.replace_each(['hello_world', 'aaa', 'hello', 'bbb']) == 'aaa bbb' |
| 385 | } |
| 386 | |
| 387 | fn test_format() { |
| 388 | template := 'First: {0}, First again: {0}, Second: {1}, Third: {2}' |
| 389 | assert template.format('A', 'B', 'C') == 'First: A, First again: A, Second: B, Third: C' |
| 390 | assert template.format('R', 'G', 'B') == 'First: R, First again: R, Second: G, Third: B' |
| 391 | assert 'Escaped {{0}} and {{braces}}'.format('unused') == 'Escaped {0} and {braces}' |
| 392 | assert '{0} {3} {1}'.format('A', 'B') == 'A {3} B' |
| 393 | assert '{10}-{2}'.format('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10') == '10-2' |
| 394 | assert 'keep {name} and {1'.format('A', 'B') == 'keep {name} and {1' |
| 395 | } |
| 396 | |
| 397 | fn test_itoa() { |
| 398 | num := 777 |
| 399 | assert num.str() == '777' |
| 400 | big := 7779998 |
| 401 | assert big.str() == '7779998' |
| 402 | a := 3 |
| 403 | assert a.str() == '3' |
| 404 | b := 5555 |
| 405 | assert b.str() == '5555' |
| 406 | zero := 0 |
| 407 | assert zero.str() == '0' |
| 408 | neg := -7 |
| 409 | assert neg.str() == '-7' |
| 410 | } |
| 411 | |
| 412 | fn test_reassign() { |
| 413 | a := 'hi' |
| 414 | mut b := a |
| 415 | b += '!' |
| 416 | assert a == 'hi' |
| 417 | assert b == 'hi!' |
| 418 | } |
| 419 | |
| 420 | fn test_runes() { |
| 421 | s := 'привет' |
| 422 | println(s.len) |
| 423 | // assert s.len == 12 |
| 424 | s2 := 'privet' |
| 425 | assert s2.len == 6 |
| 426 | u := s.runes() |
| 427 | assert u.len == 6 |
| 428 | assert s2.substr(1, 4).len == 3 |
| 429 | assert s2.substr(1, 4) == 'riv' |
| 430 | assert s2[1..4].len == 3 |
| 431 | assert s2[1..4] == 'riv' |
| 432 | assert s2[..4].len == 4 |
| 433 | assert s2[..4] == 'priv' |
| 434 | assert s2[2..].len == 4 |
| 435 | assert s2[2..] == 'ivet' |
| 436 | assert u[1..4].string().len == 6 |
| 437 | assert u[1..4].string() == 'рив' |
| 438 | assert s2.substr(1, 2) == 'r' |
| 439 | assert u[1..2].string() == 'р' |
| 440 | assert s2.runes()[1] == `r` |
| 441 | assert u[1] == `р` |
| 442 | first := u[0] |
| 443 | last := u[u.len - 1] |
| 444 | assert first.str().len == 2 |
| 445 | assert last.str().len == 2 |
| 446 | } |
| 447 | |
| 448 | fn test_contains() { |
| 449 | s := 'view.v' |
| 450 | assert s.contains('vi') |
| 451 | assert !s.contains('random') |
| 452 | assert ''.contains('') |
| 453 | assert 'abc'.contains('') |
| 454 | } |
| 455 | |
| 456 | fn test_contains_any() { |
| 457 | assert !'team'.contains_any('i') |
| 458 | assert 'fail'.contains_any('ui') |
| 459 | assert 'ure'.contains_any('ui') |
| 460 | assert 'failure'.contains_any('ui') |
| 461 | assert !'foo'.contains_any('') |
| 462 | assert !''.contains_any('') |
| 463 | } |
| 464 | |
| 465 | fn test_contains_only() { |
| 466 | assert '23885'.contains_only('0123456789') |
| 467 | assert '23gg885'.contains_only('01g23456789') |
| 468 | assert !'hello;'.contains_only('hello') |
| 469 | assert !''.contains_only('') |
| 470 | } |
| 471 | |
| 472 | fn test_contains_any_substr() { |
| 473 | s := 'Some random text' |
| 474 | assert s.contains_any_substr(['false', 'not', 'rand']) |
| 475 | assert !s.contains_any_substr(['ABC', 'invalid']) |
| 476 | assert ''.contains_any_substr([]) |
| 477 | assert 'abc'.contains_any_substr(['']) |
| 478 | } |
| 479 | |
| 480 | fn test_arr_contains() { |
| 481 | a := ['a', 'b', 'c'] |
| 482 | assert a.contains('b') |
| 483 | ints := [1, 2, 3] |
| 484 | assert ints.contains(2) |
| 485 | } |
| 486 | |
| 487 | fn test_to_num() { |
| 488 | s := '7' |
| 489 | assert s.int() == 7 |
| 490 | assert s.u8() == 7 |
| 491 | assert s.u64() == 7 |
| 492 | f := '71.5 hasdf' |
| 493 | // QTODO |
| 494 | assert f.f32() == 71.5 |
| 495 | vals := ['9'] |
| 496 | assert vals[0].int() == 9 |
| 497 | big := '93993993939322' |
| 498 | assert big.u64() == 93993993939322 |
| 499 | assert big.i64() == 93993993939322 |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | fn test_inter_format_string() { |
| 504 | float_num := 1.52345 |
| 505 | float_num_string := '-${float_num:.3f}-' |
| 506 | //assert float_num_string == '-1.523-' |
| 507 | int_num := 7 |
| 508 | int_num_string := '-${int_num:03d}-' |
| 509 | //assert int_num_string == '-007-' |
| 510 | ch := `a` |
| 511 | ch_string := '-${ch:c}-' |
| 512 | //assert ch_string == '-a-' |
| 513 | hex_n := 192 |
| 514 | hex_n_string := '-${hex_n:x}-' |
| 515 | assert hex_n_string == '-c0-' |
| 516 | oct_n := 192 |
| 517 | oct_n_string := '-${oct_n:o}-' |
| 518 | assert oct_n_string == '-300-' |
| 519 | str := 'abc' |
| 520 | str_string := '-${str:s}-' |
| 521 | assert str_string == '-abc-' |
| 522 | }*/ |
| 523 | |
| 524 | /* |
| 525 | fn test_hash() { |
| 526 | s := '10000' |
| 527 | assert s.hash() == 46730161 |
| 528 | s2 := '24640' |
| 529 | assert s2.hash() == 47778736 |
| 530 | s3 := 'Content-Type' |
| 531 | assert s3.hash() == 949037134 |
| 532 | s4 := 'bad_key' |
| 533 | assert s4.hash() == -346636507 |
| 534 | s5 := '24640' |
| 535 | // From a map collision test |
| 536 | assert s5.hash() % ((1 << 20) - 1) == s.hash() % ((1 << 20) - 1) |
| 537 | assert s5.hash() % ((1 << 20) - 1) == 592861 |
| 538 | }*/ |
| 539 | fn test_trim() { |
| 540 | assert 'banana'.trim('bna') == '' |
| 541 | assert 'abc'.trim('ac') == 'b' |
| 542 | assert 'aaabccc'.trim('ac') == 'b' |
| 543 | } |
| 544 | |
| 545 | fn test_trim_left() { |
| 546 | mut s := 'module main' |
| 547 | assert s.trim_left(' ') == 'module main' |
| 548 | s = ' module main' |
| 549 | assert s.trim_left(' ') == 'module main' |
| 550 | // test cutset |
| 551 | s = 'banana' |
| 552 | assert s.trim_left('ba') == 'nana' |
| 553 | assert s.trim_left('ban') == '' |
| 554 | } |
| 555 | |
| 556 | fn test_trim_right() { |
| 557 | mut s := 'module main' |
| 558 | assert s.trim_right(' ') == 'module main' |
| 559 | s = 'module main ' |
| 560 | assert s.trim_right(' ') == 'module main' |
| 561 | // test cutset |
| 562 | s = 'banana' |
| 563 | assert s.trim_right('na') == 'b' |
| 564 | assert s.trim_right('ban') == '' |
| 565 | } |
| 566 | |
| 567 | fn test_all_before() { |
| 568 | s := 'fn hello fn' |
| 569 | assert s.all_before(' ') == 'fn' |
| 570 | assert s.all_before('2') == s |
| 571 | assert s.all_before('') == s |
| 572 | } |
| 573 | |
| 574 | fn test_all_before_last() { |
| 575 | s := 'fn hello fn' |
| 576 | assert s.all_before_last(' ') == 'fn hello' |
| 577 | assert s.all_before_last('2') == s |
| 578 | assert s.all_before_last('') == s |
| 579 | } |
| 580 | |
| 581 | fn test_all_after() { |
| 582 | s := 'fn hello' |
| 583 | assert s.all_after('fn ') == 'hello' |
| 584 | assert s.all_after('test') == s |
| 585 | assert s.all_after('') == s |
| 586 | assert s.after('e') == 'llo' |
| 587 | x := s.after('e') |
| 588 | assert x == 'llo' |
| 589 | } |
| 590 | |
| 591 | fn test_reverse() { |
| 592 | println('hello'.reverse()) |
| 593 | assert 'hello'.reverse() == 'olleh' |
| 594 | assert ''.reverse() == '' |
| 595 | assert 'a'.reverse() == 'a' |
| 596 | } |
| 597 | |
| 598 | fn test_count() { |
| 599 | assert ''.count('') == 0 |
| 600 | assert ''.count('a') == 0 |
| 601 | assert 'a'.count('') == 0 |
| 602 | assert 'aa'.count('a') == 2 |
| 603 | assert 'aa'.count('aa') == 1 |
| 604 | assert 'aabbaa'.count('aa') == 2 |
| 605 | assert 'bbaabb'.count('aa') == 1 |
| 606 | } |
| 607 | |
| 608 | fn test_lower() { |
| 609 | mut s := 'A' |
| 610 | assert !s.is_lower() |
| 611 | assert s.to_lower() == 'a' |
| 612 | assert s.to_lower().len == 1 |
| 613 | s = 'HELLO' |
| 614 | assert !s.is_lower() |
| 615 | assert s.to_lower() == 'hello' |
| 616 | assert s.to_lower().len == 5 |
| 617 | s = 'Aloha' |
| 618 | assert !s.is_lower() |
| 619 | assert s.to_lower() == 'aloha' |
| 620 | s = 'Have A nice Day!' |
| 621 | assert !s.is_lower() |
| 622 | assert s.to_lower() == 'have a nice day!' |
| 623 | s = 'hi' |
| 624 | assert s.is_lower() |
| 625 | assert s.to_lower() == 'hi' |
| 626 | assert 'aloha!'[0] == `a` |
| 627 | assert 'aloha!'[5] == `!` |
| 628 | } |
| 629 | |
| 630 | fn test_upper() { |
| 631 | mut s := 'a' |
| 632 | assert !s.is_upper() |
| 633 | assert s.to_upper() == 'A' |
| 634 | assert s.to_upper().len == 1 |
| 635 | s = 'hello' |
| 636 | assert !s.is_upper() |
| 637 | assert s.to_upper() == 'HELLO' |
| 638 | assert s.to_upper().len == 5 |
| 639 | s = 'Aloha' |
| 640 | assert !s.is_upper() |
| 641 | assert s.to_upper() == 'ALOHA' |
| 642 | s = 'have a nice day!' |
| 643 | assert !s.is_upper() |
| 644 | assert s.to_upper() == 'HAVE A NICE DAY!' |
| 645 | s = 'HI' |
| 646 | assert s.is_upper() |
| 647 | assert s.to_upper() == 'HI' |
| 648 | } |
| 649 | |
| 650 | fn test_capitalize() { |
| 651 | mut s := 'hello' |
| 652 | assert !s.is_capital() |
| 653 | assert s.capitalize() == 'Hello' |
| 654 | s = 'test' |
| 655 | assert !s.is_capital() |
| 656 | assert s.capitalize() == 'Test' |
| 657 | s = 'i am ray' |
| 658 | assert !s.is_capital() |
| 659 | assert s.capitalize() == 'I am ray' |
| 660 | s = '' |
| 661 | assert !s.is_capital() |
| 662 | assert s.capitalize() == '' |
| 663 | s = 'TEST IT' |
| 664 | assert !s.is_capital() |
| 665 | assert s.capitalize() == 'TEST IT' |
| 666 | s = 'Test it' |
| 667 | assert s.is_capital() |
| 668 | assert s.capitalize() == 'Test it' |
| 669 | assert 'GameMission_t'.capitalize() == 'GameMission_t' |
| 670 | } |
| 671 | |
| 672 | fn test_title() { |
| 673 | mut s := 'hello world' |
| 674 | assert !s.is_title() |
| 675 | assert s.title() == 'Hello World' |
| 676 | s = 'HELLO WORLD' |
| 677 | assert !s.is_title() |
| 678 | assert s.title() == 'HELLO WORLD' |
| 679 | s = 'Hello World' |
| 680 | assert s.is_title() |
| 681 | assert s.title() == 'Hello World' |
| 682 | } |
| 683 | |
| 684 | fn test_for_loop() { |
| 685 | mut i := 0 |
| 686 | s := 'abcd' |
| 687 | |
| 688 | for c in s { |
| 689 | assert c == s[i] |
| 690 | i++ |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | fn test_for_loop_two() { |
| 695 | s := 'abcd' |
| 696 | |
| 697 | for i, c in s { |
| 698 | assert c == s[i] |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | fn test_quote() { |
| 703 | a := `'` |
| 704 | println(a) |
| 705 | println('testing double quotes') |
| 706 | b := 'hi' |
| 707 | assert b == 'hi' |
| 708 | // assert a.str() == "'" |
| 709 | } |
| 710 | |
| 711 | fn test_limit() { |
| 712 | s := 'hello' |
| 713 | assert s.limit(2) == 'he' |
| 714 | assert s.limit(9) == s |
| 715 | assert s.limit(0) == '' |
| 716 | // assert s.limit(-1) == '' |
| 717 | } |
| 718 | |
| 719 | fn test_repeat() { |
| 720 | s1 := 'V! ' |
| 721 | assert s1.repeat(5) == 'V! V! V! V! V! ' |
| 722 | assert s1.repeat(1) == s1 |
| 723 | assert s1.repeat(0) == '' |
| 724 | s2 := '' |
| 725 | assert s2.repeat(5) == s2 |
| 726 | assert s2.repeat(1) == s2 |
| 727 | assert s2.repeat(0) == s2 |
| 728 | // TODO: Add test for negative values |
| 729 | } |
| 730 | |
| 731 | fn test_starts_with() { |
| 732 | s := 'V Programming Language' |
| 733 | assert s.starts_with('V') == true |
| 734 | assert s.starts_with('V Programming') == true |
| 735 | assert s.starts_with('Language') == false |
| 736 | } |
| 737 | |
| 738 | fn test_starts_with_capital() { |
| 739 | assert 'A sentence'.starts_with_capital() |
| 740 | assert 'A paragraph. It also does.'.starts_with_capital() |
| 741 | assert ''.starts_with_capital() == false |
| 742 | assert 'no'.starts_with_capital() == false |
| 743 | assert ' No'.starts_with_capital() == false |
| 744 | } |
| 745 | |
| 746 | fn test_trim_string_left() { |
| 747 | s := 'V Programming Language' |
| 748 | assert s.trim_string_left('V ') == 'Programming Language' |
| 749 | assert s.trim_string_left('V Programming ') == 'Language' |
| 750 | assert s.trim_string_left('Language') == s |
| 751 | |
| 752 | s2 := 'TestTestTest' |
| 753 | assert s2.trim_string_left('Test') == 'TestTest' |
| 754 | assert s2.trim_string_left('TestTest') == 'Test' |
| 755 | |
| 756 | s3 := '123Test123Test' |
| 757 | assert s3.trim_string_left('123') == 'Test123Test' |
| 758 | assert s3.trim_string_left('123Test') == '123Test' |
| 759 | } |
| 760 | |
| 761 | fn test_trim_string_right() { |
| 762 | s := 'V Programming Language' |
| 763 | assert s.trim_string_right(' Language') == 'V Programming' |
| 764 | assert s.trim_string_right(' Programming Language') == 'V' |
| 765 | assert s.trim_string_right('V') == s |
| 766 | |
| 767 | s2 := 'TestTestTest' |
| 768 | assert s2.trim_string_right('Test') == 'TestTest' |
| 769 | assert s2.trim_string_right('TestTest') == 'Test' |
| 770 | |
| 771 | s3 := '123Test123Test' |
| 772 | assert s3.trim_string_right('123') == s3 |
| 773 | assert s3.trim_string_right('123Test') == '123Test' |
| 774 | } |
| 775 | |
| 776 | fn test_raw() { |
| 777 | raw := r'raw\nstring' |
| 778 | lines := raw.split('\n') |
| 779 | println(lines) |
| 780 | assert lines.len == 1 |
| 781 | println('raw string: "${raw}"') |
| 782 | |
| 783 | raw2 := r'Hello V\0' |
| 784 | assert raw2[7] == `\\` |
| 785 | assert raw2[8] == `0` |
| 786 | |
| 787 | raw3 := r'Hello V\x00' |
| 788 | assert raw3[7] == `\\` |
| 789 | assert raw3[8] == `x` |
| 790 | assert raw3[9] == `0` |
| 791 | assert raw3[10] == `0` |
| 792 | } |
| 793 | |
| 794 | fn test_raw_with_quotes() { |
| 795 | raw := r"some'" + r'"thing' // " should be escaped in the generated C code |
| 796 | println(raw) |
| 797 | // assert raw[0] == `s` |
| 798 | // assert raw[5] == `"` |
| 799 | // assert raw[6] == `t` |
| 800 | } |
| 801 | |
| 802 | fn test_escape() { |
| 803 | a := 10 |
| 804 | println("\"${a}") |
| 805 | // assert "\"${a}" == '"10' |
| 806 | } |
| 807 | |
| 808 | fn test_atoi() { |
| 809 | assert '234232'.int() == 234232 |
| 810 | assert '-9009'.int() == -9009 |
| 811 | assert '0'.int() == 0 |
| 812 | for n in -10000 .. 100000 { |
| 813 | s := n.str() |
| 814 | assert s.int() == n |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | fn test_raw_inter() { |
| 819 | world := 'world' |
| 820 | println(world) |
| 821 | s := r'hello\n$world' |
| 822 | assert s == r'hello\n$world' |
| 823 | assert s.contains('$') |
| 824 | } |
| 825 | |
| 826 | fn test_c_r() { |
| 827 | // This used to break because of r'' and c'' |
| 828 | c := 42 |
| 829 | println('${c}') |
| 830 | r := 50 |
| 831 | println('${r}') |
| 832 | } |
| 833 | |
| 834 | fn test_inter_before_comptime_if() { |
| 835 | s := '123' |
| 836 | // This used to break ('123 $....') |
| 837 | $if linux { |
| 838 | println(s) |
| 839 | } |
| 840 | assert s == '123' |
| 841 | } |
| 842 | |
| 843 | fn test_double_quote_inter() { |
| 844 | a := 1 |
| 845 | b := 2 |
| 846 | println('${a} ${b}') |
| 847 | assert '${a} ${b}' == '1 2' |
| 848 | assert '${a} ${b}' == '1 2' |
| 849 | } |
| 850 | |
| 851 | fn foo(b u8) u8 { |
| 852 | return b - 10 |
| 853 | } |
| 854 | |
| 855 | fn filter(b u8) bool { |
| 856 | return b != `a` |
| 857 | } |
| 858 | |
| 859 | fn test_split_into_lines() { |
| 860 | line_content := 'line content' |
| 861 | |
| 862 | text_cr := '${line_content}\r${line_content}\r${line_content}' |
| 863 | lines_cr := text_cr.split_into_lines() |
| 864 | |
| 865 | assert lines_cr.len == 3 |
| 866 | for line in lines_cr { |
| 867 | assert line == line_content |
| 868 | } |
| 869 | |
| 870 | text_crlf := '${line_content}\r\n${line_content}\r\n${line_content}' |
| 871 | lines_crlf := text_crlf.split_into_lines() |
| 872 | |
| 873 | assert lines_crlf.len == 3 |
| 874 | for line in lines_crlf { |
| 875 | assert line == line_content |
| 876 | } |
| 877 | |
| 878 | text_lf := '${line_content}\n${line_content}\n${line_content}' |
| 879 | lines_lf := text_lf.split_into_lines() |
| 880 | |
| 881 | assert lines_lf.len == 3 |
| 882 | for line in lines_lf { |
| 883 | assert line == line_content |
| 884 | } |
| 885 | |
| 886 | text_mixed := '${line_content}\n${line_content}\r${line_content}' |
| 887 | lines_mixed := text_mixed.split_into_lines() |
| 888 | |
| 889 | assert lines_mixed.len == 3 |
| 890 | for line in lines_mixed { |
| 891 | assert line == line_content |
| 892 | } |
| 893 | |
| 894 | text_mixed_trailers := '${line_content}\n${line_content}\r${line_content}\r\r\r\n\n\n\r\r' |
| 895 | lines_mixed_trailers := text_mixed_trailers.split_into_lines() |
| 896 | |
| 897 | assert lines_mixed_trailers.len == 9 |
| 898 | for line in lines_mixed_trailers { |
| 899 | assert line == line_content || line == '' |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | fn test_string_literal_with_backslash() { |
| 904 | a := 'HelloWorld' |
| 905 | assert a == 'HelloWorld' |
| 906 | |
| 907 | b := 'OneTwoThree' |
| 908 | assert b == 'OneTwoThree' |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | type MyString = string |
| 913 | |
| 914 | fn test_string_alias() { |
| 915 | s := MyString('hi') |
| 916 | ss := s + '!' |
| 917 | } |
| 918 | */ |
| 919 | |
| 920 | // sort an array of structs, by their string field values |
| 921 | |
| 922 | struct Ka { |
| 923 | s string |
| 924 | i int |
| 925 | } |
| 926 | |
| 927 | fn test_sorter() { |
| 928 | mut arr := [ |
| 929 | Ka{ |
| 930 | s: 'bbb' |
| 931 | i: 100 |
| 932 | }, |
| 933 | Ka{ |
| 934 | s: 'aaa' |
| 935 | i: 101 |
| 936 | }, |
| 937 | Ka{ |
| 938 | s: 'ccc' |
| 939 | i: 102 |
| 940 | }, |
| 941 | ] |
| 942 | cmp := fn (a &Ka, b &Ka) int { |
| 943 | return compare_strings(a.s, b.s) |
| 944 | } |
| 945 | arr.sort_with_compare(cmp) |
| 946 | assert arr[0].s == 'aaa' |
| 947 | assert arr[0].i == 101 |
| 948 | assert arr[1].s == 'bbb' |
| 949 | assert arr[1].i == 100 |
| 950 | assert arr[2].s == 'ccc' |
| 951 | assert arr[2].i == 102 |
| 952 | } |
| 953 | |
| 954 | fn test_fields() { |
| 955 | assert 'a bcde'.fields() == ['a', 'bcde'] |
| 956 | assert ' sss \t ssss '.fields() == ['sss', 'ssss'] |
| 957 | assert '\n xyz \t abc def'.fields() == ['xyz', 'abc', 'def'] |
| 958 | assert 'hello'.fields() == ['hello'] |
| 959 | assert ''.fields() == [] |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | fn test_interpolation_after_quoted_variable_still_works() { |
| 964 | rr := 'abc' |
| 965 | tt := 'xyz' |
| 966 | |
| 967 | // Basic interpolation, no internal quotes |
| 968 | yy := 'Replacing ${rr} with ${tt}' |
| 969 | assert yy == 'Replacing abc with xyz' |
| 970 | |
| 971 | // Interpolation after quoted variable ending with 'r'quote |
| 972 | // that may be mistaken with the start of a raw string, |
| 973 | // ensure that it is not. |
| 974 | ss := 'Replacing "${rr}" with "${tt}"' |
| 975 | assert ss == 'Replacing "abc" with "xyz"' |
| 976 | zz := "Replacing '${rr}' with '${tt}'" |
| 977 | assert zz == "Replacing 'abc' with 'xyz'" |
| 978 | |
| 979 | // Interpolation after quoted variable ending with 'c'quote |
| 980 | // may be mistaken with the start of a c string, so |
| 981 | // check it is not. |
| 982 | cc := 'abc' |
| 983 | ccc := "Replacing '${cc}' with '${tt}'" |
| 984 | assert ccc == "Replacing 'abc' with 'xyz'" |
| 985 | cccq := 'Replacing "${cc}" with "${tt}"' |
| 986 | assert cccq == 'Replacing "abc" with "xyz"' |
| 987 | } |
| 988 | */ |
| 989 | fn test_index_any() { |
| 990 | x := 'abcdefghij' |
| 991 | assert x.index_any('ef') == 4 |
| 992 | assert x.index_any('fe') == 4 |
| 993 | } |
| 994 | |
| 995 | fn test_js_string() { |
| 996 | s := js'hello V' |
| 997 | assert s.charAt(JS.Number(0)) == js'h' |
| 998 | assert s.charAt(JS.Number(6)) == js'V' |
| 999 | assert s.charCodeAt(JS.Number(0)) == JS.Number(104) |
| 1000 | assert s.toUpperCase() == js'HELLO V' |
| 1001 | assert s.toLowerCase() == js'hello v' |
| 1002 | assert s.concat(js' from JS') == js'hello V from JS' |
| 1003 | assert s.includes(js' ') == JS.Boolean(true) |
| 1004 | assert s.startsWith(js'hello') == JS.Boolean(true) |
| 1005 | assert s.endsWith(js'V') == JS.Boolean(true) |
| 1006 | } |
| 1007 | |
| 1008 | fn test_tos_from_u8_ptr() { |
| 1009 | mut buf := [u8(`0`), `0`, `0`, `0`, `-`, `0`, `0`, `-`, `0`, `0`, `T`, `0`, `0`, `:`, `0`, |
| 1010 | `0`, `:`, `0`, `0`, `.`, `0`, `0`, `0`, `0`, `0`, `0`, `0`, `0`, `0`, `Z`]! |
| 1011 | s := unsafe { tos(&buf[0], buf.len) } |
| 1012 | |
| 1013 | assert s == '0000-00-00T00:00:00.000000000Z' |
| 1014 | } |
| 1015 | |