]> gitweb.factorcode.org Git - factor.git/blob - core/strings/strings-tests.factor
use radix literals
[factor.git] / core / strings / strings-tests.factor
1 USING: continuations kernel math math.order namespaces make
2 strings strings.private sbufs tools.test sequences vectors
3 arrays memory prettyprint io.streams.null ;
4 IN: strings.tests
5
6 [ CHAR: b ] [ 1 >bignum "abc" nth ] unit-test
7
8 [ ] [ 10 [ [ -1000000 <sbuf> ] ignore-errors ] times ] unit-test
9
10 [ "abc" ] [ [ "a" "b" "c" ] [ [ % ] each ] "" make ] unit-test
11
12 [ "abc" ] [ "ab" "c" append ] unit-test
13 [ "abc" ] [ "a" "b" "c" 3append ] unit-test
14
15 [ 3 ] [ "a" "hola" start ] unit-test
16 [ f ] [ "x" "hola" start ] unit-test
17 [ 0 ] [ "" "a" start ] unit-test
18 [ 0 ] [ "" "" start ] unit-test
19 [ 0 ] [ "hola" "hola" start ] unit-test
20 [ 1 ] [ "ol" "hola" start ] unit-test
21 [ f ] [ "amigo" "hola" start ] unit-test
22 [ f ] [ "holaa" "hola" start ] unit-test
23
24 [ "Beginning" ] [ "Beginning and end" 9 head ] unit-test
25
26 [ f ] [ CHAR: I "team" member? ] unit-test
27 [ t ] [ "ea" "team" subseq? ] unit-test
28 [ f ] [ "actore" "Factor" subseq? ] unit-test
29
30 [ "end" ] [ "Beginning and end" 14 tail ] unit-test
31
32 [ t ] [ "abc" "abd" before? ] unit-test
33 [ t ] [ "z" "abd" after? ] unit-test
34 [ "abc" ] [ "abc" "abd" min ] unit-test
35 [ "z" ] [ "z" "abd" max ] unit-test
36
37 [ 0 10 "hello" subseq ] must-fail
38
39 [ "Replacing+spaces+with+plus" ]
40 [
41     "Replacing spaces with plus"
42     [ dup CHAR: \s = [ drop CHAR: + ] when ] map
43 ]
44 unit-test
45
46 [ "05" ] [ "5" 2 CHAR: 0 pad-head ] unit-test
47 [ "666" ] [ "666" 2 CHAR: 0 pad-head ] unit-test
48
49 [ 1 "" nth ] must-fail
50 [ -6 "hello" nth ] must-fail
51
52 [ t ] [ "hello world" dup >vector >string = ] unit-test 
53
54 [ "ab" ] [ 2 "abc" resize-string ] unit-test
55 [ "abc\0\0\0" ] [ 6 "abc" resize-string ] unit-test
56
57 [ "\u001234b" ] [ 2 "\u001234bc" resize-string ] unit-test
58 [ "\u001234bc\0\0\0" ] [ 6 "\u001234bc" resize-string ] unit-test
59
60 ! Random tester found this
61 [ 2 -7 resize-string ] [ { "kernel-error" 3 11 -7 } = ] must-fail-with
62
63 ! Make sure 24-bit strings work
64 "hello world" "s" set
65
66 [ ] [ 0x1234 1 "s" get set-nth ] unit-test
67 [ 0x1234 ] [ 1 "s" get nth ] unit-test
68
69 [ ] [ 0x4321 3 "s" get set-nth ] unit-test
70 [ 0x4321 ] [ 3 "s" get nth ] unit-test
71
72 [ ] [ 0x654321 5 "s" get set-nth ] unit-test
73 [ 0x654321 ] [ 5 "s" get nth ] unit-test
74
75 [
76     {
77         CHAR: h
78         0x1234
79         CHAR: l
80         0x4321
81         CHAR: o
82         0x654321
83         CHAR: w
84         CHAR: o
85         CHAR: r
86         CHAR: l
87         CHAR: d
88     }
89 ] [
90     "s" get >array
91 ] unit-test
92
93 ! Make sure string initialization works
94 [ 0x123456 ] [ 100 0x123456 <string> first ] unit-test
95
96 ! Make sure we clear aux vector when storing octets
97 [ "\u123456hi" ] [ "ih\u123456" clone reverse! ] unit-test
98
99 ! Make sure aux vector is not shared
100 [ "\udeadbe" ] [
101     "\udeadbe" clone
102     CHAR: \u123456 over clone set-first
103 ] unit-test
104
105 ! Regressions
106 [ ] [
107     [
108         4 [
109             100 [ "obdurak" clone ] replicate
110             gc
111             dup [
112                 1234 0 rot set-string-nth
113             ] each
114             1000 [
115                 1000 f <array> drop
116             ] times
117             .
118         ] times
119     ] with-null-writer
120 ] unit-test
121
122 [ t ] [
123     10000 [
124         drop
125         300 100 CHAR: \u123456
126         [ <string> clone resize-string first ] keep =
127     ] all-integers?
128 ] unit-test
129
130 "X" "s" set
131 [ ] [ 0x100,0000 0 "s" get set-nth ] unit-test
132 [ 0 ] [ 0 "s" get nth ] unit-test
133
134 [ ] [ -1 0 "s" get set-nth ] unit-test
135 [ 0x7fffff ] [ 0 "s" get nth ] unit-test