add using
[factor/jcg.git] / core / strings / strings-tests.factor
blob810e9051d8637b3a2543fcbab03f6db70148cf60
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
6 [ CHAR: b ] [ 1 >bignum "abc" nth ] unit-test
8 [ ] [ 10 [ [ -1000000 <sbuf> ] ignore-errors ] times ] unit-test
10 [ "abc" ] [ [ "a" "b" "c" ] [ [ % ] each ] "" make ] unit-test
12 [ "abc" ] [ "ab" "c" append ] unit-test
13 [ "abc" ] [ "a" "b" "c" 3append ] unit-test
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
24 [ "Beginning" ] [ "Beginning and end" 9 head ] unit-test
26 [ f ] [ CHAR: I "team" member? ] unit-test
27 [ t ] [ "ea" "team" subseq? ] unit-test
28 [ f ] [ "actore" "Factor" subseq? ] unit-test
30 [ "end" ] [ "Beginning and end" 14 tail ] unit-test
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
37 [ 0 10 "hello" subseq ] must-fail
39 [ "Replacing+spaces+with+plus" ]
41     "Replacing spaces with plus"
42     [ dup CHAR: \s = [ drop CHAR: + ] when ] map
44 unit-test
46 [ "05" ] [ "5" 2 CHAR: 0 pad-left ] unit-test
47 [ "666" ] [ "666" 2 CHAR: 0 pad-left ] unit-test
49 [ 1 "" nth ] must-fail
50 [ -6 "hello" nth ] must-fail
52 [ t ] [ "hello world" dup >vector >string = ] unit-test 
54 [ "ab" ] [ 2 "abc" resize-string ] unit-test
55 [ "abc\0\0\0" ] [ 6 "abc" resize-string ] unit-test
57 [ "\u001234b" ] [ 2 "\u001234bc" resize-string ] unit-test
58 [ "\u001234bc\0\0\0" ] [ 6 "\u001234bc" resize-string ] unit-test
60 ! Random tester found this
61 [ 2 -7 resize-string ] [ { "kernel-error" 3 12 -7 } = ] must-fail-with
63 ! Make sure 24-bit strings work
64 "hello world" "s" set
66 [ ] [ HEX: 1234 1 "s" get set-nth ] unit-test
67 [ ] [ HEX: 4321 3 "s" get set-nth ] unit-test
68 [ ] [ HEX: 654321 5 "s" get set-nth ] unit-test
71     {
72         CHAR: h
73         HEX: 1234
74         CHAR: l
75         HEX: 4321
76         CHAR: o
77         HEX: 654321
78         CHAR: w
79         CHAR: o
80         CHAR: r
81         CHAR: l
82         CHAR: d
83     }
84 ] [
85     "s" get >array
86 ] unit-test
88 ! Make sure we clear aux vector when storing octets
89 [ "\u123456hi" ] [ "ih\u123456" clone dup reverse-here ] unit-test
91 ! Make sure aux vector is not shared
92 [ "\udeadbe" ] [
93     "\udeadbe" clone
94     CHAR: \u123456 over clone set-first
95 ] unit-test
97 ! Regressions
98 [ ] [
99     [
100         4 [
101             100 [ "obdurak" clone ] replicate
102             gc
103             dup [
104                 1234 0 rot set-string-nth
105             ] each
106             1000 [
107                 1000 f <array> drop
108             ] times
109             .
110         ] times
111     ] with-null-writer
112 ] unit-test
114 [ t ] [
115     10000 [
116         drop
117         300 100 CHAR: \u123456
118         [ <string> clone resize-string first ] keep =
119     ] all?
120 ] unit-test