summary refs log tree commit diff stats
path: root/tests/stdlib/tstring.nim
blob: fcbacc5339e17739998577c228272676e0c0078d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
discard """
  targets: "c cpp js"
"""

const characters = "abcdefghijklmnopqrstuvwxyz"
const numbers = "1234567890"

proc test_string_slice() =
  # test "slice of length == len(characters)":
  # replace characters completely by numbers
  var s: string
  s = characters
  s[0..^1] = numbers
  doAssert s == numbers

  # test "slice of length > len(numbers)":
  # replace characters by slice of same length
  s = characters
  s[1..16] = numbers
  doAssert s == "a1234567890rstuvwxyz"

  # test "slice of length == len(numbers)":
  # replace characters by slice of same length
  s = characters
  s[1..10] = numbers
  doAssert s == "a1234567890lmnopqrstuvwxyz"

  # test "slice of length < len(numbers)":
  # replace slice of length. and insert remaining chars
  s = characters
  s[1..4] = numbers
  doAssert s == "a1234567890fghijklmnopqrstuvwxyz"

  # test "slice of length == 1":
  # replace first character. and insert remaining 9 chars
  s = characters
  s[1..1] = numbers
  doAssert s == "a1234567890cdefghijklmnopqrstuvwxyz"

  # test "slice of length == 0":
  # insert chars at slice start index
  s = characters
  s[2..1] = numbers
  doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"

  # test "slice of negative length":
  # same as slice of zero length
  s = characters
  s[2..0] = numbers
  doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"

  when nimvm:
    discard
  else:
    # bug #6223
    doAssertRaises(IndexDefect):
      discard s[0..999]


proc test_string_cmp() =
  let world = "hello\0world"
  let earth = "hello\0earth"
  let short = "hello\0"
  let hello = "hello"
  let goodbye = "goodbye"

  doAssert world == world
  doAssert world != earth
  doAssert world != short
  doAssert world != hello
  doAssert world != goodbye

  doAssert cmp(world, world) == 0
  doAssert cmp(world, earth) > 0
  doAssert cmp(world, short) > 0
  doAssert cmp(world, hello) > 0
  doAssert cmp(world, goodbye) > 0


#--------------------------
# bug #7816
import sugar
import sequtils

proc tester[T](x: T) =
  let test = toSeq(0..4).map(i => newSeq[int]())
  doAssert $test == "@[@[], @[], @[], @[], @[]]"



# #14497 
func reverse*(a: string): string =
  result = a
  for i in 0 ..< a.len div 2:
    swap(result[i], result[^(i + 1)])


proc main() =
  # xxx put all tests here to test in VM and RT
  test_string_slice()
  test_string_cmp()

  tester(1)

  block: # reverse
    doAssert reverse("hello") == "olleh"

  block: # len, high
    var a = "ab\0cd"
    var b = a.cstring
    doAssert a.len == 5
    block: # bug #16405
      when defined(js):
        when nimvm: doAssert b.len == 2
        else: doAssert b.len == 5
      else: doAssert b.len == 2

    doAssert a.high == a.len - 1
    doAssert b.high == b.len - 1

    doAssert "".len == 0
    doAssert "".high == -1
    doAssert "".cstring.len == 0
    doAssert "".cstring.high == -1

    var c: cstring = nil
    template impl() =
      doAssert c.len == 0
      doAssert c.high == -1
    when defined js:
      when nimvm: impl()
      else:
        # xxx pending bug #16674
        discard
    else: impl()

static: main()
main()