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