summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorParashurama <Rhagdamaziel@ymail.com>2017-02-01 12:13:01 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-02-01 12:13:01 +0100
commitc57fcf42df4206229de10c8ed0463fe94517bd4b (patch)
tree6e0e306ed40c3d49d2a30fb6cc50873a513a2d07 /tests/stdlib
parentd90f3f59aca668d3000d6fd64199bfe720240911 (diff)
downloadNim-c57fcf42df4206229de10c8ed0463fe94517bd4b.tar.gz
fix string slice & splice (#5311)
code fixes courtesy of @memophen
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/tstring.nim55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim
new file mode 100644
index 000000000..ddf533a17
--- /dev/null
+++ b/tests/stdlib/tstring.nim
@@ -0,0 +1,55 @@
+discard """
+  file: "tstring.nim"
+  output: "OK"
+"""
+const characters = "abcdefghijklmnopqrstuvwxyz"
+const numbers = "1234567890"
+
+var s: string
+
+proc test_string_slice() =
+  # test "slice of length == len(characters)":
+  # replace characters completely by numbers
+  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"
+
+  echo("OK")
+
+test_string_slice()