2021-03-16 06:29:14 +01:00
|
|
|
|
import strings
|
|
|
|
|
|
2022-01-04 10:21:08 +01:00
|
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
2020-03-26 17:03:14 +01:00
|
|
|
|
struct Foo {
|
|
|
|
|
bar int
|
|
|
|
|
mut:
|
|
|
|
|
str string
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_add() {
|
|
|
|
|
mut a := 'a'
|
|
|
|
|
a += 'b'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert a == ('ab')
|
2019-06-23 03:12:56 +02:00
|
|
|
|
a = 'a'
|
|
|
|
|
for i := 1; i < 1000; i++ {
|
|
|
|
|
a += 'b'
|
|
|
|
|
}
|
|
|
|
|
assert a.len == 1000
|
|
|
|
|
assert a.ends_with('bbbbb')
|
|
|
|
|
a += '123'
|
|
|
|
|
assert a.ends_with('3')
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 09:23:57 +02:00
|
|
|
|
fn test_len_utf8() {
|
|
|
|
|
assert 'Vlang'.len_utf8() == 5
|
|
|
|
|
assert 'María'.len_utf8() == 5
|
|
|
|
|
assert '姓名'.len_utf8() == 2
|
|
|
|
|
assert 'Слово'.len_utf8() == 5
|
|
|
|
|
assert 'Λέξη'.len_utf8() == 4
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_ends_with() {
|
|
|
|
|
a := 'browser.v'
|
|
|
|
|
assert a.ends_with('.v')
|
2020-05-15 19:37:14 +02:00
|
|
|
|
|
|
|
|
|
s := 'V Programming Language'
|
|
|
|
|
assert s.ends_with('guage') == true
|
|
|
|
|
assert s.ends_with('Language') == true
|
|
|
|
|
assert s.ends_with('Programming Language') == true
|
|
|
|
|
assert s.ends_with('V') == false
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_between() {
|
2021-03-16 06:29:14 +01:00
|
|
|
|
s := 'hello [man] how you doing'
|
2019-06-23 03:12:56 +02:00
|
|
|
|
assert s.find_between('[', ']') == 'man'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_compare() {
|
|
|
|
|
a := 'Music'
|
|
|
|
|
b := 'src'
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert b >= a
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 20:25:50 +02:00
|
|
|
|
fn test_lt() {
|
|
|
|
|
a := ''
|
|
|
|
|
b := 'a'
|
|
|
|
|
c := 'a'
|
|
|
|
|
d := 'b'
|
|
|
|
|
e := 'aa'
|
|
|
|
|
f := 'ab'
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert a < b
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert !(b < c)
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert c < d
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert !(d < e)
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert c < e
|
|
|
|
|
assert e < f
|
2019-06-23 20:25:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_ge() {
|
|
|
|
|
a := 'aa'
|
|
|
|
|
b := 'aa'
|
|
|
|
|
c := 'ab'
|
|
|
|
|
d := 'abc'
|
|
|
|
|
e := 'aaa'
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert b >= a
|
|
|
|
|
assert c >= b
|
|
|
|
|
assert d >= c
|
2019-09-21 15:26:25 +02:00
|
|
|
|
assert !(c >= d)
|
2021-04-12 15:01:03 +02:00
|
|
|
|
assert e >= a
|
2019-06-23 20:25:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_compare_strings() {
|
|
|
|
|
a := 'aa'
|
|
|
|
|
b := 'aa'
|
|
|
|
|
c := 'ab'
|
|
|
|
|
d := 'abc'
|
|
|
|
|
e := 'aaa'
|
|
|
|
|
assert compare_strings(a, b) == 0
|
|
|
|
|
assert compare_strings(b, c) == -1
|
|
|
|
|
assert compare_strings(c, d) == -1
|
|
|
|
|
assert compare_strings(d, e) == 1
|
|
|
|
|
assert compare_strings(a, e) == -1
|
|
|
|
|
assert compare_strings(e, a) == 1
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_sort() {
|
|
|
|
|
mut vals := [
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'arr',
|
|
|
|
|
'an',
|
|
|
|
|
'a',
|
|
|
|
|
'any',
|
2019-06-23 03:12:56 +02:00
|
|
|
|
]
|
|
|
|
|
len := vals.len
|
|
|
|
|
vals.sort()
|
|
|
|
|
assert len == vals.len
|
2019-06-23 20:25:50 +02:00
|
|
|
|
assert vals[0] == 'a'
|
|
|
|
|
assert vals[1] == 'an'
|
|
|
|
|
assert vals[2] == 'any'
|
|
|
|
|
assert vals[3] == 'arr'
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-06 20:32:32 +02:00
|
|
|
|
fn test_sort_reverse() {
|
|
|
|
|
mut vals := [
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'arr',
|
|
|
|
|
'an',
|
|
|
|
|
'a',
|
|
|
|
|
'any',
|
2020-10-06 20:32:32 +02:00
|
|
|
|
]
|
|
|
|
|
len := vals.len
|
2021-03-16 06:29:14 +01:00
|
|
|
|
vals.sort(b > a)
|
2020-10-06 20:32:32 +02:00
|
|
|
|
assert len == vals.len
|
2021-03-04 11:19:02 +01:00
|
|
|
|
assert vals[0] == 'a'
|
|
|
|
|
assert vals[1] == 'an'
|
|
|
|
|
assert vals[2] == 'any'
|
|
|
|
|
assert vals[3] == 'arr'
|
2020-10-06 20:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 11:03:10 +01:00
|
|
|
|
fn test_ranges() {
|
|
|
|
|
s := 'test'
|
|
|
|
|
s1 := s[0..20] or { 'both' }
|
|
|
|
|
s2 := s[..20] or { 'last' }
|
|
|
|
|
s3 := s[10..] or { 'first' }
|
|
|
|
|
s4 := ranges_propagate_both(s) or { 'both' }
|
|
|
|
|
s5 := ranges_propagate_last(s) or { 'last' }
|
|
|
|
|
s6 := ranges_propagate_first(s) or { 'first' }
|
|
|
|
|
assert s1 == 'both'
|
|
|
|
|
assert s2 == 'last'
|
|
|
|
|
assert s3 == 'first'
|
|
|
|
|
assert s4 == 'both'
|
|
|
|
|
assert s5 == 'last'
|
|
|
|
|
assert s6 == 'first'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ranges_propagate_first(s string) ?string {
|
|
|
|
|
return s[10..] ?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ranges_propagate_last(s string) ?string {
|
|
|
|
|
return s[..20] ?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ranges_propagate_both(s string) ?string {
|
|
|
|
|
return s[1..20] ?
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-01 14:10:13 +01:00
|
|
|
|
fn test_split_nth() {
|
2021-03-16 06:29:14 +01:00
|
|
|
|
a := '1,2,3'
|
|
|
|
|
assert a.split(',').len == 3
|
|
|
|
|
assert a.split_nth(',', -1).len == 3
|
|
|
|
|
assert a.split_nth(',', 0).len == 3
|
|
|
|
|
assert a.split_nth(',', 1).len == 1
|
|
|
|
|
assert a.split_nth(',', 2).len == 2
|
|
|
|
|
assert a.split_nth(',', 10).len == 3
|
|
|
|
|
b := '1::2::3'
|
|
|
|
|
assert b.split('::').len == 3
|
|
|
|
|
assert b.split_nth('::', -1).len == 3
|
|
|
|
|
assert b.split_nth('::', 0).len == 3
|
|
|
|
|
assert b.split_nth('::', 1).len == 1
|
|
|
|
|
assert b.split_nth('::', 2).len == 2
|
|
|
|
|
assert b.split_nth('::', 10).len == 3
|
|
|
|
|
c := 'ABCDEF'
|
2020-03-26 17:03:14 +01:00
|
|
|
|
println(c.split('').len)
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert c.split('').len == 6
|
|
|
|
|
assert c.split_nth('', 3).len == 3
|
|
|
|
|
assert c.split_nth('BC', -1).len == 2
|
|
|
|
|
d := ','
|
|
|
|
|
assert d.split(',').len == 2
|
|
|
|
|
assert d.split_nth('', 3).len == 1
|
|
|
|
|
assert d.split_nth(',', -1).len == 2
|
|
|
|
|
assert d.split_nth(',', 3).len == 2
|
|
|
|
|
e := ',,,0,,,,,a,,b,'
|
|
|
|
|
assert e.split(',,').len == 5
|
|
|
|
|
assert e.split_nth(',,', 3).len == 3
|
|
|
|
|
assert e.split_nth(',', -1).len == 12
|
|
|
|
|
assert e.split_nth(',', 3).len == 3
|
2019-12-01 14:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 20:12:36 +01:00
|
|
|
|
fn test_split_nth_values() {
|
|
|
|
|
line := 'CMD=eprintln(phase=1)'
|
2020-01-25 19:12:36 +01:00
|
|
|
|
|
2020-01-24 20:12:36 +01:00
|
|
|
|
a0 := line.split_nth('=', 0)
|
|
|
|
|
assert a0.len == 3
|
|
|
|
|
assert a0[0] == 'CMD'
|
|
|
|
|
assert a0[1] == 'eprintln(phase'
|
|
|
|
|
assert a0[2] == '1)'
|
|
|
|
|
|
|
|
|
|
a1 := line.split_nth('=', 1)
|
|
|
|
|
assert a1.len == 1
|
|
|
|
|
assert a1[0] == 'CMD=eprintln(phase=1)'
|
|
|
|
|
|
|
|
|
|
a2 := line.split_nth('=', 2)
|
|
|
|
|
assert a2.len == 2
|
|
|
|
|
assert a2[0] == 'CMD'
|
|
|
|
|
assert a2[1] == 'eprintln(phase=1)'
|
|
|
|
|
|
|
|
|
|
a3 := line.split_nth('=', 3)
|
|
|
|
|
assert a3.len == 3
|
|
|
|
|
assert a3[0] == 'CMD'
|
|
|
|
|
assert a3[1] == 'eprintln(phase'
|
|
|
|
|
assert a3[2] == '1)'
|
2020-01-25 19:12:36 +01:00
|
|
|
|
|
|
|
|
|
a4 := line.split_nth('=', 4)
|
|
|
|
|
assert a4.len == 3
|
|
|
|
|
assert a4[0] == 'CMD'
|
|
|
|
|
assert a4[1] == 'eprintln(phase'
|
|
|
|
|
assert a4[2] == '1)'
|
2020-01-24 20:12:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_split() {
|
|
|
|
|
mut s := 'volt/twitch.v:34'
|
|
|
|
|
mut vals := s.split(':')
|
|
|
|
|
assert vals.len == 2
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert vals[0] == 'volt/twitch.v'
|
|
|
|
|
assert vals[1] == '34'
|
2019-11-28 14:49:35 +01:00
|
|
|
|
// /////////
|
2019-06-23 03:12:56 +02:00
|
|
|
|
s = '2018-01-01z13:01:02'
|
|
|
|
|
vals = s.split('z')
|
|
|
|
|
assert vals.len == 2
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert vals[0] == '2018-01-01'
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert vals[1] == '13:01:02'
|
|
|
|
|
// //////////
|
2019-06-23 03:12:56 +02:00
|
|
|
|
s = '4627a862c3dec29fb3182a06b8965e0025759e18___1530207969___blue'
|
|
|
|
|
vals = s.split('___')
|
|
|
|
|
assert vals.len == 3
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert vals[0] == '4627a862c3dec29fb3182a06b8965e0025759e18'
|
|
|
|
|
assert vals[1] == '1530207969'
|
|
|
|
|
assert vals[2] == 'blue'
|
2019-07-31 07:26:22 +02:00
|
|
|
|
// /////////
|
|
|
|
|
s = 'lalala'
|
2019-08-18 14:02:30 +02:00
|
|
|
|
vals = s.split('a')
|
2019-12-01 14:10:13 +01:00
|
|
|
|
assert vals.len == 4
|
2019-08-18 14:02:30 +02:00
|
|
|
|
assert vals[0] == 'l'
|
|
|
|
|
assert vals[1] == 'l'
|
|
|
|
|
assert vals[2] == 'l'
|
2019-12-01 14:10:13 +01:00
|
|
|
|
assert vals[3] == ''
|
2019-11-10 17:37:36 +01:00
|
|
|
|
// /////////
|
|
|
|
|
s = 'awesome'
|
|
|
|
|
a := s.split('')
|
|
|
|
|
assert a.len == 7
|
|
|
|
|
assert a[0] == 'a'
|
|
|
|
|
assert a[1] == 'w'
|
|
|
|
|
assert a[2] == 'e'
|
|
|
|
|
assert a[3] == 's'
|
|
|
|
|
assert a[4] == 'o'
|
|
|
|
|
assert a[5] == 'm'
|
|
|
|
|
assert a[6] == 'e'
|
2020-12-08 09:51:47 +01:00
|
|
|
|
// /////////
|
|
|
|
|
s = 'wavy turquoise bags'
|
|
|
|
|
vals = s.split(' bags')
|
|
|
|
|
assert vals.len == 2
|
|
|
|
|
assert vals[0] == 'wavy turquoise'
|
|
|
|
|
assert vals[1] == ''
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 19:13:40 +01:00
|
|
|
|
fn test_split_any() {
|
|
|
|
|
assert 'ABC'.split_any('') == ['A', 'B', 'C']
|
|
|
|
|
assert ''.split_any(' ') == []
|
|
|
|
|
assert ' '.split_any(' ') == ['']
|
|
|
|
|
assert ' '.split_any(' ') == ['', '']
|
|
|
|
|
assert 'Ciao come stai? '.split_any(' ') == ['Ciao', 'come', 'stai?']
|
|
|
|
|
assert 'Ciao+come*stai? '.split_any('+*') == ['Ciao', 'come', 'stai? ']
|
|
|
|
|
assert 'Ciao+come*stai? '.split_any('+* ') == ['Ciao', 'come', 'stai?']
|
|
|
|
|
assert 'first row\nsecond row'.split_any(' \n') == ['first', 'row', 'second', 'row']
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_trim_space() {
|
|
|
|
|
a := ' a '
|
|
|
|
|
assert a.trim_space() == 'a'
|
|
|
|
|
code := '
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
println(2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
'
|
|
|
|
|
code_clean := 'fn main() {
|
|
|
|
|
println(2)
|
|
|
|
|
}'
|
|
|
|
|
assert code.trim_space() == code_clean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_join() {
|
2021-03-16 06:29:14 +01:00
|
|
|
|
mut strings := ['a', 'b', 'c']
|
2019-06-23 03:12:56 +02:00
|
|
|
|
mut s := strings.join(' ')
|
|
|
|
|
assert s == 'a b c'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
strings = [
|
|
|
|
|
'one
|
2019-06-23 03:12:56 +02:00
|
|
|
|
two ',
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'three!
|
|
|
|
|
four!',
|
|
|
|
|
]
|
2019-06-23 03:12:56 +02:00
|
|
|
|
s = strings.join(' ')
|
|
|
|
|
assert s.contains('one') && s.contains('two ') && s.contains('four')
|
2021-03-16 06:29:14 +01:00
|
|
|
|
empty := []string{len: 0}
|
2020-10-18 00:48:06 +02:00
|
|
|
|
assert empty.join('A') == ''
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_clone() {
|
|
|
|
|
mut a := 'a'
|
|
|
|
|
a += 'a'
|
|
|
|
|
a += 'a'
|
2019-07-25 14:06:26 +02:00
|
|
|
|
b := a
|
2019-07-25 14:13:35 +02:00
|
|
|
|
c := a.clone()
|
2019-06-23 03:12:56 +02:00
|
|
|
|
assert c == a
|
|
|
|
|
assert c == 'aaa'
|
|
|
|
|
assert b == 'aaa'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_replace() {
|
|
|
|
|
a := 'hello man!'
|
|
|
|
|
mut b := a.replace('man', 'world')
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert b == ('hello world!')
|
2019-06-23 03:12:56 +02:00
|
|
|
|
b = b.replace('!', '')
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert b == ('hello world')
|
2019-06-23 03:12:56 +02:00
|
|
|
|
b = b.replace('h', 'H')
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert b == ('Hello world')
|
2019-07-10 09:48:10 +02:00
|
|
|
|
b = b.replace('foo', 'bar')
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert b == ('Hello world')
|
2019-06-23 03:12:56 +02:00
|
|
|
|
s := 'hey man how are you'
|
|
|
|
|
assert s.replace('man ', '') == 'hey how are you'
|
|
|
|
|
lol := 'lol lol lol'
|
2019-07-10 09:48:10 +02:00
|
|
|
|
assert lol.replace('lol', 'LOL') == 'LOL LOL LOL'
|
2019-06-23 03:12:56 +02:00
|
|
|
|
b = 'oneBtwoBBthree'
|
|
|
|
|
assert b.replace('B', '') == 'onetwothree'
|
2019-12-01 08:33:26 +01:00
|
|
|
|
b = '*charptr'
|
|
|
|
|
assert b.replace('charptr', 'byteptr') == '*byteptr'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
c := 'abc'
|
|
|
|
|
assert c.replace('', '-') == c
|
|
|
|
|
v := 'a b c d'
|
|
|
|
|
assert v.replace(' ', ' ') == 'a b c d'
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 12:32:12 +01:00
|
|
|
|
fn test_replace_each() {
|
|
|
|
|
s := 'hello man man :)'
|
|
|
|
|
q := s.replace_each([
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'man',
|
|
|
|
|
'dude',
|
|
|
|
|
'hello',
|
|
|
|
|
'hey',
|
2019-12-10 12:32:12 +01:00
|
|
|
|
])
|
|
|
|
|
assert q == 'hey dude dude :)'
|
|
|
|
|
bb := '[b]bold[/b] [code]code[/code]'
|
|
|
|
|
assert bb.replace_each([
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'[b]',
|
|
|
|
|
'<b>',
|
|
|
|
|
'[/b]',
|
|
|
|
|
'</b>',
|
|
|
|
|
'[code]',
|
|
|
|
|
'<code>',
|
|
|
|
|
'[/code]',
|
|
|
|
|
'</code>',
|
2019-12-10 12:32:12 +01:00
|
|
|
|
]) == '<b>bold</b> <code>code</code>'
|
2019-12-11 01:20:30 +01:00
|
|
|
|
bb2 := '[b]cool[/b]'
|
|
|
|
|
assert bb2.replace_each([
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'[b]',
|
|
|
|
|
'<b>',
|
|
|
|
|
'[/b]',
|
|
|
|
|
'</b>',
|
2019-12-11 01:20:30 +01:00
|
|
|
|
]) == '<b>cool</b>'
|
2020-09-12 02:31:06 +02:00
|
|
|
|
t := 'aaaaaaaa'
|
|
|
|
|
y := t.replace_each([
|
2021-03-16 06:29:14 +01:00
|
|
|
|
'aa',
|
|
|
|
|
'b',
|
2020-09-12 02:31:06 +02:00
|
|
|
|
])
|
|
|
|
|
assert y == 'bbbb'
|
2021-03-22 09:46:45 +01:00
|
|
|
|
s2 := 'hello_world hello'
|
|
|
|
|
assert s2.replace_each(['hello_world', 'aaa', 'hello', 'bbb']) == 'aaa bbb'
|
2019-12-10 12:32:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_itoa() {
|
|
|
|
|
num := 777
|
|
|
|
|
assert num.str() == '777'
|
|
|
|
|
big := 7779998
|
|
|
|
|
assert big.str() == '7779998'
|
|
|
|
|
a := 3
|
|
|
|
|
assert a.str() == '3'
|
|
|
|
|
b := 5555
|
|
|
|
|
assert b.str() == '5555'
|
|
|
|
|
zero := 0
|
|
|
|
|
assert zero.str() == '0'
|
|
|
|
|
neg := -7
|
|
|
|
|
assert neg.str() == '-7'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_reassign() {
|
|
|
|
|
a := 'hi'
|
|
|
|
|
mut b := a
|
|
|
|
|
b += '!'
|
|
|
|
|
assert a == 'hi'
|
|
|
|
|
assert b == 'hi!'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_runes() {
|
|
|
|
|
s := 'привет'
|
|
|
|
|
assert s.len == 12
|
|
|
|
|
s2 := 'privet'
|
|
|
|
|
assert s2.len == 6
|
2021-07-03 19:14:09 +02:00
|
|
|
|
u := s.runes()
|
2019-06-23 03:12:56 +02:00
|
|
|
|
assert u.len == 6
|
|
|
|
|
assert s2.substr(1, 4).len == 3
|
|
|
|
|
assert s2.substr(1, 4) == 'riv'
|
2019-10-27 00:37:29 +02:00
|
|
|
|
assert s2[1..4].len == 3
|
|
|
|
|
assert s2[1..4] == 'riv'
|
2019-10-27 07:36:04 +01:00
|
|
|
|
assert s2[..4].len == 4
|
|
|
|
|
assert s2[..4] == 'priv'
|
|
|
|
|
assert s2[2..].len == 4
|
|
|
|
|
assert s2[2..] == 'ivet'
|
2021-07-03 19:14:09 +02:00
|
|
|
|
assert u[1..4].string().len == 6
|
|
|
|
|
assert u[1..4].string() == 'рив'
|
2019-06-23 03:12:56 +02:00
|
|
|
|
assert s2.substr(1, 2) == 'r'
|
2021-07-03 19:14:09 +02:00
|
|
|
|
assert u[1..2].string() == 'р'
|
|
|
|
|
assert s2.runes()[1] == `r`
|
|
|
|
|
assert u[1] == `р`
|
|
|
|
|
first := u[0]
|
|
|
|
|
last := u[u.len - 1]
|
|
|
|
|
assert first.str().len == 2
|
|
|
|
|
assert last.str().len == 2
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_contains() {
|
|
|
|
|
s := 'view.v'
|
|
|
|
|
assert s.contains('vi')
|
|
|
|
|
assert !s.contains('random')
|
2020-06-14 11:24:15 +02:00
|
|
|
|
assert ''.contains('')
|
|
|
|
|
assert 'abc'.contains('')
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 21:48:50 +02:00
|
|
|
|
fn test_contains_any() {
|
|
|
|
|
assert !'team'.contains_any('i')
|
|
|
|
|
assert 'fail'.contains_any('ui')
|
|
|
|
|
assert 'ure'.contains_any('ui')
|
|
|
|
|
assert 'failure'.contains_any('ui')
|
|
|
|
|
assert !'foo'.contains_any('')
|
|
|
|
|
assert !''.contains_any('')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_contains_any_substr() {
|
|
|
|
|
s := 'Some random text'
|
|
|
|
|
assert s.contains_any_substr(['false', 'not', 'rand'])
|
|
|
|
|
assert !s.contains_any_substr(['ABC', 'invalid'])
|
|
|
|
|
assert ''.contains_any_substr([])
|
|
|
|
|
assert 'abc'.contains_any_substr([''])
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_arr_contains() {
|
|
|
|
|
a := ['a', 'b', 'c']
|
|
|
|
|
assert a.contains('b')
|
|
|
|
|
ints := [1, 2, 3]
|
|
|
|
|
assert ints.contains(2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_to_num() {
|
|
|
|
|
s := '7'
|
2019-06-25 14:56:34 +02:00
|
|
|
|
assert s.int() == 7
|
2022-04-15 13:58:56 +02:00
|
|
|
|
assert s.u8() == 7
|
2019-10-15 03:26:19 +02:00
|
|
|
|
assert s.u64() == 7
|
2019-06-23 03:12:56 +02:00
|
|
|
|
f := '71.5 hasdf'
|
2020-04-02 10:15:35 +02:00
|
|
|
|
// QTODO
|
2020-04-22 09:35:14 +02:00
|
|
|
|
assert f.f32() == 71.5
|
2019-06-23 03:12:56 +02:00
|
|
|
|
vals := ['9']
|
2019-06-25 14:56:34 +02:00
|
|
|
|
assert vals[0].int() == 9
|
2019-10-15 03:26:19 +02:00
|
|
|
|
big := '93993993939322'
|
|
|
|
|
assert big.u64() == 93993993939322
|
|
|
|
|
assert big.i64() == 93993993939322
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 17:25:39 +02:00
|
|
|
|
fn test_inter_format_string() {
|
|
|
|
|
float_num := 1.52345
|
2021-03-16 06:29:14 +01:00
|
|
|
|
float_num_string := '-${float_num:.3f}-'
|
2020-04-11 17:25:39 +02:00
|
|
|
|
assert float_num_string == '-1.523-'
|
|
|
|
|
int_num := 7
|
|
|
|
|
int_num_string := '-${int_num:03d}-'
|
|
|
|
|
assert int_num_string == '-007-'
|
|
|
|
|
ch := `a`
|
|
|
|
|
ch_string := '-${ch:c}-'
|
|
|
|
|
assert ch_string == '-a-'
|
|
|
|
|
hex_n := 192
|
|
|
|
|
hex_n_string := '-${hex_n:x}-'
|
|
|
|
|
assert hex_n_string == '-c0-'
|
|
|
|
|
oct_n := 192
|
|
|
|
|
oct_n_string := '-${oct_n:o}-'
|
|
|
|
|
assert oct_n_string == '-300-'
|
|
|
|
|
str := 'abc'
|
|
|
|
|
str_string := '-${str:s}-'
|
|
|
|
|
assert str_string == '-abc-'
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_hash() {
|
|
|
|
|
s := '10000'
|
|
|
|
|
assert s.hash() == 46730161
|
|
|
|
|
s2 := '24640'
|
|
|
|
|
assert s2.hash() == 47778736
|
|
|
|
|
s3 := 'Content-Type'
|
|
|
|
|
assert s3.hash() == 949037134
|
|
|
|
|
s4 := 'bad_key'
|
|
|
|
|
assert s4.hash() == -346636507
|
|
|
|
|
s5 := '24640'
|
|
|
|
|
// From a map collision test
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert s5.hash() % ((1 << 20) - 1) == s.hash() % ((1 << 20) - 1)
|
|
|
|
|
assert s5.hash() % ((1 << 20) - 1) == 592861
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 06:53:56 +02:00
|
|
|
|
fn test_trim() {
|
|
|
|
|
assert 'banana'.trim('bna') == ''
|
|
|
|
|
assert 'abc'.trim('ac') == 'b'
|
|
|
|
|
assert 'aaabccc'.trim('ac') == 'b'
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_trim_left() {
|
|
|
|
|
mut s := 'module main'
|
|
|
|
|
assert s.trim_left(' ') == 'module main'
|
|
|
|
|
s = ' module main'
|
|
|
|
|
assert s.trim_left(' ') == 'module main'
|
2019-08-26 13:18:58 +02:00
|
|
|
|
// test cutset
|
|
|
|
|
s = 'banana'
|
|
|
|
|
assert s.trim_left('ba') == 'nana'
|
2020-04-06 14:30:25 +02:00
|
|
|
|
assert s.trim_left('ban') == ''
|
2019-06-23 03:12:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 19:07:40 +02:00
|
|
|
|
fn test_trim_right() {
|
|
|
|
|
mut s := 'module main'
|
|
|
|
|
assert s.trim_right(' ') == 'module main'
|
|
|
|
|
s = 'module main '
|
|
|
|
|
assert s.trim_right(' ') == 'module main'
|
2019-08-26 13:18:58 +02:00
|
|
|
|
// test cutset
|
|
|
|
|
s = 'banana'
|
|
|
|
|
assert s.trim_right('na') == 'b'
|
2020-04-06 14:30:25 +02:00
|
|
|
|
assert s.trim_right('ban') == ''
|
2019-08-17 19:07:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 20:14:21 +01:00
|
|
|
|
fn test_all_before() {
|
|
|
|
|
s := 'fn hello fn'
|
|
|
|
|
assert s.all_before(' ') == 'fn'
|
|
|
|
|
assert s.all_before('2') == s
|
|
|
|
|
assert s.all_before('') == s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_all_before_last() {
|
|
|
|
|
s := 'fn hello fn'
|
|
|
|
|
assert s.all_before_last(' ') == 'fn hello'
|
|
|
|
|
assert s.all_before_last('2') == s
|
|
|
|
|
assert s.all_before_last('') == s
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:12:56 +02:00
|
|
|
|
fn test_all_after() {
|
|
|
|
|
s := 'fn hello'
|
2020-02-20 20:14:21 +01:00
|
|
|
|
assert s.all_after('fn ') == 'hello'
|
|
|
|
|
assert s.all_after('test') == s
|
|
|
|
|
assert s.all_after('') == s
|
2020-05-18 05:10:56 +02:00
|
|
|
|
assert s.after('e') == 'llo'
|
|
|
|
|
x := s.after('e')
|
|
|
|
|
assert x == 'llo'
|
2019-06-26 19:03:35 +02:00
|
|
|
|
}
|
2019-06-25 14:56:34 +02:00
|
|
|
|
|
2019-07-07 17:43:34 +02:00
|
|
|
|
fn test_reverse() {
|
2020-02-20 20:14:21 +01:00
|
|
|
|
assert 'hello'.reverse() == 'olleh'
|
|
|
|
|
assert ''.reverse() == ''
|
|
|
|
|
assert 'a'.reverse() == 'a'
|
2019-07-07 17:43:34 +02:00
|
|
|
|
}
|
2019-07-16 17:59:07 +02:00
|
|
|
|
|
2019-07-18 04:42:00 +02:00
|
|
|
|
fn test_bytes_to_string() {
|
2020-03-04 17:39:27 +01:00
|
|
|
|
mut buf := vcalloc(10)
|
2020-07-22 20:42:51 +02:00
|
|
|
|
unsafe {
|
|
|
|
|
buf[0] = `h`
|
|
|
|
|
buf[1] = `e`
|
|
|
|
|
buf[2] = `l`
|
|
|
|
|
buf[3] = `l`
|
|
|
|
|
buf[4] = `o`
|
2020-07-29 21:48:50 +02:00
|
|
|
|
}
|
2020-08-10 18:05:26 +02:00
|
|
|
|
assert unsafe { buf.vstring() } == 'hello'
|
|
|
|
|
assert unsafe { buf.vstring_with_len(2) } == 'he'
|
2022-04-15 13:58:56 +02:00
|
|
|
|
bytes := [u8(`h`), `e`, `l`, `l`, `o`]
|
2020-08-10 18:05:26 +02:00
|
|
|
|
assert bytes.bytestr() == 'hello'
|
2019-08-18 14:02:30 +02:00
|
|
|
|
}
|
2020-11-14 18:43:42 +01:00
|
|
|
|
|
|
|
|
|
fn test_charptr() {
|
2021-04-04 16:43:32 +02:00
|
|
|
|
foo := &char('VLANG'.str)
|
2021-01-07 20:32:02 +01:00
|
|
|
|
println(typeof(foo).name)
|
2021-04-04 16:43:32 +02:00
|
|
|
|
assert typeof(foo).name == '&char'
|
2020-11-14 18:43:42 +01:00
|
|
|
|
assert unsafe { foo.vstring() } == 'VLANG'
|
|
|
|
|
assert unsafe { foo.vstring_with_len(3) } == 'VLA'
|
|
|
|
|
}
|
2019-08-03 22:24:03 +02:00
|
|
|
|
|
|
|
|
|
fn test_count() {
|
|
|
|
|
assert ''.count('') == 0
|
|
|
|
|
assert ''.count('a') == 0
|
|
|
|
|
assert 'a'.count('') == 0
|
|
|
|
|
assert 'aa'.count('a') == 2
|
|
|
|
|
assert 'aa'.count('aa') == 1
|
|
|
|
|
assert 'aabbaa'.count('aa') == 2
|
|
|
|
|
assert 'bbaabb'.count('aa') == 1
|
|
|
|
|
}
|
2019-08-26 12:32:53 +02:00
|
|
|
|
|
2020-04-12 12:09:05 +02:00
|
|
|
|
fn test_lower() {
|
|
|
|
|
mut s := 'A'
|
|
|
|
|
assert !s.is_lower()
|
|
|
|
|
assert s.to_lower() == 'a'
|
|
|
|
|
assert s.to_lower().len == 1
|
|
|
|
|
s = 'HELLO'
|
|
|
|
|
assert !s.is_lower()
|
|
|
|
|
assert s.to_lower() == 'hello'
|
|
|
|
|
assert s.to_lower().len == 5
|
|
|
|
|
s = 'Aloha'
|
|
|
|
|
assert !s.is_lower()
|
|
|
|
|
assert s.to_lower() == 'aloha'
|
|
|
|
|
s = 'Have A nice Day!'
|
|
|
|
|
assert !s.is_lower()
|
|
|
|
|
assert s.to_lower() == 'have a nice day!'
|
|
|
|
|
s = 'hi'
|
|
|
|
|
assert s.is_lower()
|
|
|
|
|
assert s.to_lower() == 'hi'
|
|
|
|
|
assert 'aloha!'[0] == `a`
|
|
|
|
|
assert 'aloha!'[5] == `!`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_upper() {
|
|
|
|
|
mut s := 'a'
|
|
|
|
|
assert !s.is_upper()
|
|
|
|
|
assert s.to_upper() == 'A'
|
|
|
|
|
assert s.to_upper().len == 1
|
|
|
|
|
s = 'hello'
|
|
|
|
|
assert !s.is_upper()
|
|
|
|
|
assert s.to_upper() == 'HELLO'
|
|
|
|
|
assert s.to_upper().len == 5
|
|
|
|
|
s = 'Aloha'
|
|
|
|
|
assert !s.is_upper()
|
|
|
|
|
assert s.to_upper() == 'ALOHA'
|
|
|
|
|
s = 'have a nice day!'
|
|
|
|
|
assert !s.is_upper()
|
|
|
|
|
assert s.to_upper() == 'HAVE A NICE DAY!'
|
|
|
|
|
s = 'HI'
|
|
|
|
|
assert s.is_upper()
|
|
|
|
|
assert s.to_upper() == 'HI'
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 12:32:53 +02:00
|
|
|
|
fn test_capitalize() {
|
|
|
|
|
mut s := 'hello'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
assert !s.is_capital()
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert s.capitalize() == 'Hello'
|
|
|
|
|
s = 'test'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
assert !s.is_capital()
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert s.capitalize() == 'Test'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
s = 'i am ray'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
assert !s.is_capital()
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert s.capitalize() == 'I am ray'
|
2020-02-20 11:33:38 +01:00
|
|
|
|
s = ''
|
2020-04-12 12:09:05 +02:00
|
|
|
|
assert !s.is_capital()
|
2020-02-20 11:33:38 +01:00
|
|
|
|
assert s.capitalize() == ''
|
2020-04-12 12:09:05 +02:00
|
|
|
|
s = 'TEST IT'
|
|
|
|
|
assert !s.is_capital()
|
2020-08-09 03:57:54 +02:00
|
|
|
|
assert s.capitalize() == 'TEST IT'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
s = 'Test it'
|
|
|
|
|
assert s.is_capital()
|
|
|
|
|
assert s.capitalize() == 'Test it'
|
2020-08-09 03:57:54 +02:00
|
|
|
|
assert 'GameMission_t'.capitalize() == 'GameMission_t'
|
2019-08-26 12:32:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_title() {
|
2020-04-12 12:09:05 +02:00
|
|
|
|
mut s := 'hello world'
|
|
|
|
|
assert !s.is_title()
|
2019-08-26 12:32:53 +02:00
|
|
|
|
assert s.title() == 'Hello World'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
s = 'HELLO WORLD'
|
|
|
|
|
assert !s.is_title()
|
2020-08-09 04:09:48 +02:00
|
|
|
|
assert s.title() == 'HELLO WORLD'
|
2020-04-12 12:09:05 +02:00
|
|
|
|
s = 'Hello World'
|
|
|
|
|
assert s.is_title()
|
2019-09-21 15:26:25 +02:00
|
|
|
|
assert s.title() == 'Hello World'
|
|
|
|
|
}
|
2019-08-29 18:46:03 +02:00
|
|
|
|
|
|
|
|
|
fn test_for_loop() {
|
|
|
|
|
mut i := 0
|
|
|
|
|
s := 'abcd'
|
|
|
|
|
|
|
|
|
|
for c in s {
|
|
|
|
|
assert c == s[i]
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_for_loop_two() {
|
|
|
|
|
s := 'abcd'
|
|
|
|
|
|
|
|
|
|
for i, c in s {
|
|
|
|
|
assert c == s[i]
|
|
|
|
|
}
|
2019-09-02 14:02:25 +02:00
|
|
|
|
}
|
2019-09-11 14:21:20 +02:00
|
|
|
|
|
|
|
|
|
fn test_quote() {
|
2021-08-06 05:21:28 +02:00
|
|
|
|
a := `'`
|
2021-03-16 06:29:14 +01:00
|
|
|
|
println('testing double quotes')
|
|
|
|
|
b := 'hi'
|
2019-09-21 15:26:25 +02:00
|
|
|
|
assert b == 'hi'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert a.str() == "'"
|
2019-09-11 14:21:20 +02:00
|
|
|
|
}
|
2019-09-20 18:07:38 +02:00
|
|
|
|
|
2020-02-20 20:14:21 +01:00
|
|
|
|
fn test_limit() {
|
|
|
|
|
s := 'hello'
|
|
|
|
|
assert s.limit(2) == 'he'
|
|
|
|
|
assert s.limit(9) == s
|
|
|
|
|
assert s.limit(0) == ''
|
|
|
|
|
// assert s.limit(-1) == ''
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 21:54:53 +02:00
|
|
|
|
fn test_repeat() {
|
2020-02-29 15:25:31 +01:00
|
|
|
|
s1 := 'V! '
|
|
|
|
|
assert s1.repeat(5) == 'V! V! V! V! V! '
|
|
|
|
|
assert s1.repeat(1) == s1
|
|
|
|
|
assert s1.repeat(0) == ''
|
|
|
|
|
s2 := ''
|
|
|
|
|
assert s2.repeat(5) == s2
|
|
|
|
|
assert s2.repeat(1) == s2
|
|
|
|
|
assert s2.repeat(0) == s2
|
|
|
|
|
// TODO Add test for negative values
|
2019-09-26 21:54:53 +02:00
|
|
|
|
}
|
2019-10-06 02:56:08 +02:00
|
|
|
|
|
2020-05-15 19:37:14 +02:00
|
|
|
|
fn test_starts_with() {
|
|
|
|
|
s := 'V Programming Language'
|
|
|
|
|
assert s.starts_with('V') == true
|
|
|
|
|
assert s.starts_with('V Programming') == true
|
|
|
|
|
assert s.starts_with('Language') == false
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
|
fn test_starts_with_capital() {
|
|
|
|
|
assert 'A sentence'.starts_with_capital()
|
|
|
|
|
assert 'A paragraph. It also does.'.starts_with_capital()
|
|
|
|
|
assert ''.starts_with_capital() == false
|
|
|
|
|
assert 'no'.starts_with_capital() == false
|
|
|
|
|
assert ' No'.starts_with_capital() == false
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 11:49:22 +01:00
|
|
|
|
fn test_trim_string_left() {
|
2020-05-15 19:37:14 +02:00
|
|
|
|
s := 'V Programming Language'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s.trim_string_left('V ') == 'Programming Language'
|
|
|
|
|
assert s.trim_string_left('V Programming ') == 'Language'
|
|
|
|
|
assert s.trim_string_left('Language') == s
|
2020-06-30 15:44:53 +02:00
|
|
|
|
|
|
|
|
|
s2 := 'TestTestTest'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s2.trim_string_left('Test') == 'TestTest'
|
|
|
|
|
assert s2.trim_string_left('TestTest') == 'Test'
|
2020-06-30 15:44:53 +02:00
|
|
|
|
|
|
|
|
|
s3 := '123Test123Test'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s3.trim_string_left('123') == 'Test123Test'
|
|
|
|
|
assert s3.trim_string_left('123Test') == '123Test'
|
2020-05-15 19:37:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 11:49:22 +01:00
|
|
|
|
fn test_trim_string_right() {
|
2020-05-15 19:37:14 +02:00
|
|
|
|
s := 'V Programming Language'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s.trim_string_right(' Language') == 'V Programming'
|
|
|
|
|
assert s.trim_string_right(' Programming Language') == 'V'
|
|
|
|
|
assert s.trim_string_right('V') == s
|
2020-06-30 15:44:53 +02:00
|
|
|
|
|
|
|
|
|
s2 := 'TestTestTest'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s2.trim_string_right('Test') == 'TestTest'
|
|
|
|
|
assert s2.trim_string_right('TestTest') == 'Test'
|
2020-06-30 15:44:53 +02:00
|
|
|
|
|
|
|
|
|
s3 := '123Test123Test'
|
2022-01-05 11:49:22 +01:00
|
|
|
|
assert s3.trim_string_right('123') == s3
|
|
|
|
|
assert s3.trim_string_right('123Test') == '123Test'
|
2020-05-15 19:37:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 02:56:08 +02:00
|
|
|
|
fn test_raw() {
|
|
|
|
|
raw := r'raw\nstring'
|
|
|
|
|
lines := raw.split('\n')
|
2020-04-02 10:15:35 +02:00
|
|
|
|
println(lines)
|
2019-10-06 02:56:08 +02:00
|
|
|
|
assert lines.len == 1
|
|
|
|
|
println('raw string: "$raw"')
|
2020-09-19 18:09:18 +02:00
|
|
|
|
|
|
|
|
|
raw2 := r'Hello V\0'
|
|
|
|
|
assert raw2[7] == `\\`
|
|
|
|
|
assert raw2[8] == `0`
|
2020-09-21 16:34:24 +02:00
|
|
|
|
|
|
|
|
|
raw3 := r'Hello V\x00'
|
|
|
|
|
assert raw3[7] == `\\`
|
|
|
|
|
assert raw3[8] == `x`
|
|
|
|
|
assert raw3[9] == `0`
|
2020-09-25 12:02:32 +02:00
|
|
|
|
assert raw3[10] == `0`
|
2019-11-28 14:49:35 +01:00
|
|
|
|
}
|
2019-10-07 00:09:11 +02:00
|
|
|
|
|
2020-02-09 16:29:05 +01:00
|
|
|
|
fn test_raw_with_quotes() {
|
|
|
|
|
raw := r"some'" + r'"thing' // " should be escaped in the generated C code
|
|
|
|
|
assert raw[0] == `s`
|
|
|
|
|
assert raw[5] == `"`
|
|
|
|
|
assert raw[6] == `t`
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 00:09:11 +02:00
|
|
|
|
fn test_escape() {
|
2020-10-27 15:25:44 +01:00
|
|
|
|
a := 10
|
2021-04-20 19:18:53 +02:00
|
|
|
|
println("\"$a")
|
|
|
|
|
assert "\"$a" == '"10'
|
2019-11-28 14:49:35 +01:00
|
|
|
|
}
|
2019-10-15 03:26:19 +02:00
|
|
|
|
|
2019-10-25 22:41:18 +02:00
|
|
|
|
fn test_atoi() {
|
|
|
|
|
assert '234232'.int() == 234232
|
|
|
|
|
assert '-9009'.int() == -9009
|
|
|
|
|
assert '0'.int() == 0
|
|
|
|
|
for n in -10000 .. 100000 {
|
|
|
|
|
s := n.str()
|
|
|
|
|
assert s.int() == n
|
2019-11-28 14:49:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-25 22:41:18 +02:00
|
|
|
|
|
2019-11-03 21:57:26 +01:00
|
|
|
|
fn test_raw_inter() {
|
|
|
|
|
world := 'world'
|
2019-11-06 04:57:04 +01:00
|
|
|
|
println(world)
|
2019-11-03 21:57:26 +01:00
|
|
|
|
s := r'hello\n$world'
|
|
|
|
|
assert s == r'hello\n$world'
|
|
|
|
|
assert s.contains('$')
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 20:16:58 +01:00
|
|
|
|
fn test_c_r() {
|
|
|
|
|
// This used to break because of r'' and c''
|
|
|
|
|
c := 42
|
|
|
|
|
println('$c')
|
|
|
|
|
r := 50
|
|
|
|
|
println('$r')
|
2019-12-03 11:08:57 +01:00
|
|
|
|
}
|
2019-11-28 14:49:35 +01:00
|
|
|
|
|
2021-11-15 14:47:29 +01:00
|
|
|
|
fn test_inter_before_comptime_if() {
|
2019-12-03 11:08:57 +01:00
|
|
|
|
s := '123'
|
|
|
|
|
// This used to break ('123 $....')
|
|
|
|
|
$if linux {
|
|
|
|
|
println(s)
|
|
|
|
|
}
|
2019-12-12 11:51:05 +01:00
|
|
|
|
assert s == '123'
|
2019-11-28 14:49:35 +01:00
|
|
|
|
}
|
2019-11-16 20:16:58 +01:00
|
|
|
|
|
2019-12-08 18:06:00 +01:00
|
|
|
|
fn test_double_quote_inter() {
|
|
|
|
|
a := 1
|
|
|
|
|
b := 2
|
2021-03-16 06:29:14 +01:00
|
|
|
|
println('$a $b')
|
|
|
|
|
assert '$a $b' == '1 2'
|
|
|
|
|
assert '$a $b' == '1 2'
|
2019-12-08 18:06:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 17:25:45 +02:00
|
|
|
|
fn foo(b u8) u8 {
|
2020-06-17 14:29:44 +02:00
|
|
|
|
return b - 10
|
2020-06-17 12:26:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 17:25:45 +02:00
|
|
|
|
fn filter(b u8) bool {
|
2020-07-12 20:07:28 +02:00
|
|
|
|
return b != `a`
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 09:23:57 +01:00
|
|
|
|
fn test_split_into_lines() {
|
|
|
|
|
line_content := 'Line'
|
2021-03-16 06:29:14 +01:00
|
|
|
|
text_crlf := '$line_content\r\n$line_content\r\n$line_content'
|
2020-02-09 09:23:57 +01:00
|
|
|
|
lines_crlf := text_crlf.split_into_lines()
|
|
|
|
|
|
|
|
|
|
assert lines_crlf.len == 3
|
|
|
|
|
for line in lines_crlf {
|
|
|
|
|
assert line == line_content
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:29:14 +01:00
|
|
|
|
text_lf := '$line_content\n$line_content\n$line_content'
|
2020-02-09 09:23:57 +01:00
|
|
|
|
lines_lf := text_lf.split_into_lines()
|
|
|
|
|
|
|
|
|
|
assert lines_lf.len == 3
|
|
|
|
|
for line in lines_lf {
|
|
|
|
|
assert line == line_content
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-04 10:19:23 +02:00
|
|
|
|
|
2021-03-16 06:29:14 +01:00
|
|
|
|
fn test_string_literal_with_backslash() {
|
|
|
|
|
a := 'HelloWorld'
|
|
|
|
|
assert a == 'HelloWorld'
|
2020-05-04 10:19:23 +02:00
|
|
|
|
|
2021-03-16 06:29:14 +01:00
|
|
|
|
b := 'OneTwoThree'
|
2020-05-04 10:19:23 +02:00
|
|
|
|
assert b == 'OneTwoThree'
|
|
|
|
|
}
|
2020-05-16 16:12:23 +02:00
|
|
|
|
|
|
|
|
|
/*
|
2020-09-25 12:02:32 +02:00
|
|
|
|
type MyString = string
|
2020-05-16 16:12:23 +02:00
|
|
|
|
|
|
|
|
|
fn test_string_alias() {
|
|
|
|
|
s := MyString('hi')
|
|
|
|
|
ss := s + '!'
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-07-13 18:45:13 +02:00
|
|
|
|
|
|
|
|
|
// sort an array of structs, by their string field values
|
|
|
|
|
|
|
|
|
|
struct Ka {
|
|
|
|
|
s string
|
|
|
|
|
i int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_sorter() {
|
|
|
|
|
mut arr := [
|
|
|
|
|
Ka{
|
|
|
|
|
s: 'bbb'
|
|
|
|
|
i: 100
|
|
|
|
|
},
|
|
|
|
|
Ka{
|
|
|
|
|
s: 'aaa'
|
|
|
|
|
i: 101
|
|
|
|
|
},
|
|
|
|
|
Ka{
|
|
|
|
|
s: 'ccc'
|
|
|
|
|
i: 102
|
2021-03-16 06:29:14 +01:00
|
|
|
|
},
|
2020-07-13 18:45:13 +02:00
|
|
|
|
]
|
2020-10-18 00:48:06 +02:00
|
|
|
|
cmp := fn (a &Ka, b &Ka) int {
|
2020-07-13 18:45:13 +02:00
|
|
|
|
return compare_strings(a.s, b.s)
|
|
|
|
|
}
|
|
|
|
|
arr.sort_with_compare(cmp)
|
|
|
|
|
assert arr[0].s == 'aaa'
|
|
|
|
|
assert arr[0].i == 101
|
|
|
|
|
assert arr[1].s == 'bbb'
|
|
|
|
|
assert arr[1].i == 100
|
|
|
|
|
assert arr[2].s == 'ccc'
|
|
|
|
|
assert arr[2].i == 102
|
|
|
|
|
}
|
2020-12-29 07:46:46 +01:00
|
|
|
|
|
2021-03-16 18:45:27 +01:00
|
|
|
|
fn test_fields() {
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert 'a bcde'.fields() == ['a', 'bcde']
|
|
|
|
|
assert ' sss \t ssss '.fields() == ['sss', 'ssss']
|
|
|
|
|
assert '\n xyz \t abc def'.fields() == ['xyz', 'abc', 'def']
|
2021-03-16 18:45:27 +01:00
|
|
|
|
assert 'hello'.fields() == ['hello']
|
2021-03-16 06:29:14 +01:00
|
|
|
|
assert ''.fields() == []
|
2020-12-29 07:46:46 +01:00
|
|
|
|
}
|
2021-01-28 14:38:00 +01:00
|
|
|
|
|
|
|
|
|
fn test_interpolation_after_quoted_variable_still_works() {
|
|
|
|
|
rr := 'abc'
|
|
|
|
|
tt := 'xyz'
|
|
|
|
|
|
|
|
|
|
// Basic interpolation, no internal quotes
|
|
|
|
|
yy := 'Replacing $rr with $tt'
|
|
|
|
|
assert yy == 'Replacing abc with xyz'
|
|
|
|
|
|
|
|
|
|
// Interpolation after quoted variable ending with 'r'quote
|
|
|
|
|
// that may be mistaken with the start of a raw string,
|
|
|
|
|
// ensure that it is not.
|
|
|
|
|
ss := 'Replacing "$rr" with "$tt"'
|
|
|
|
|
assert ss == 'Replacing "abc" with "xyz"'
|
|
|
|
|
zz := "Replacing '$rr' with '$tt'"
|
|
|
|
|
assert zz == "Replacing 'abc' with 'xyz'"
|
|
|
|
|
|
|
|
|
|
// Interpolation after quoted variable ending with 'c'quote
|
|
|
|
|
// may be mistaken with the start of a c string, so
|
|
|
|
|
// check it is not.
|
|
|
|
|
cc := 'abc'
|
|
|
|
|
ccc := "Replacing '$cc' with '$tt'"
|
|
|
|
|
assert ccc == "Replacing 'abc' with 'xyz'"
|
|
|
|
|
cccq := 'Replacing "$cc" with "$tt"'
|
|
|
|
|
assert cccq == 'Replacing "abc" with "xyz"'
|
|
|
|
|
}
|
2021-06-30 08:17:38 +02:00
|
|
|
|
|
|
|
|
|
fn test_emoji_to_runes() {
|
|
|
|
|
x := '👋'
|
|
|
|
|
assert x.runes()[0] == `👋`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn test_string_to_rune() {
|
|
|
|
|
x := 'Hello World 👋'
|
|
|
|
|
assert x.runes().len == 13
|
|
|
|
|
}
|
2021-09-06 14:06:37 +02:00
|
|
|
|
|
|
|
|
|
fn test_index_any() {
|
|
|
|
|
x := 'abcdefghij'
|
|
|
|
|
assert x.index_any('ef') == 4
|
|
|
|
|
assert x.index_any('fe') == 4
|
|
|
|
|
}
|
2022-01-04 16:43:45 +01:00
|
|
|
|
|
|
|
|
|
fn test_string_f64() {
|
|
|
|
|
assert ''.f64() == 0
|
|
|
|
|
assert '123'.f64() == 123
|
|
|
|
|
assert '-123'.f64() == -123
|
|
|
|
|
assert '-123.456'.f64() == -123.456
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const f32_epsilon = 0.0000000001
|
|
|
|
|
|
|
|
|
|
fn test_string_f32() {
|
|
|
|
|
assert ''.f32() - 0 <= f32_epsilon
|
|
|
|
|
assert '123'.f32() - 123 < f32_epsilon
|
|
|
|
|
assert '-123'.f32() - (-123) < f32_epsilon
|
|
|
|
|
assert '-123.456'.f32() - (-123.456) <= f32_epsilon
|
|
|
|
|
}
|
2022-02-16 22:41:05 +01:00
|
|
|
|
|
|
|
|
|
fn test_string_with_zero_byte_escape() {
|
2022-04-15 13:58:56 +02:00
|
|
|
|
assert '\x00'.bytes() == [u8(0)]
|
2022-02-16 22:41:05 +01:00
|
|
|
|
}
|