summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-01-11 01:16:20 -0800
committerGitHub <noreply@github.com>2021-01-11 10:16:20 +0100
commitf6c2450cdb7e24b5dbd118494560cc8452dd3688 (patch)
tree9c4f53378d6f32eb7e7485d3772ad21262144f1a /tests/stdlib
parent0286a0879bc44e5267a5fd36e6f4aac8f78713ea (diff)
downloadNim-f6c2450cdb7e24b5dbd118494560cc8452dd3688.tar.gz
fix #16555, fixes #16405: len, high honors '\0' for cstring in vm (#16610)
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/thashes.nim18
-rw-r--r--tests/stdlib/tstring.nim34
2 files changed, 49 insertions, 3 deletions
diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim
index 17640387a..ce7bb7d8c 100644
--- a/tests/stdlib/thashes.nim
+++ b/tests/stdlib/thashes.nim
@@ -86,8 +86,6 @@ block largeSize: # longer than 4 characters
   doAssert hash(xx, 0, 3) == hash(ssl, 0, 3)
 
 proc main() =
-
-
   doAssert hash(0.0) == hash(0)
   doAssert hash(cstring"abracadabra") == 97309975
   doAssert hash(cstring"abracadabra") == hash("abracadabra")
@@ -115,6 +113,22 @@ proc main() =
     doAssert hash(-9999.283456) != 0
     doAssert hash(84375674.0) != 0
 
+  block: # bug #16555
+    proc fn(): auto =
+      # avoids hardcoding values
+      var a = "abc\0def"
+      var b = a.cstring
+      result = (hash(a), hash(b))
+      doAssert result[0] != result[1]
+    when not defined(js):
+      doAssert fn() == static(fn())
+    else:
+      # xxx this is a tricky case; consistency of hashes for cstring's containing
+      # '\0\' matters for c backend but less for js backend since such strings
+      # are much less common in js backend; we make vm for js backend consistent
+      # with c backend instead of js backend because FFI code (or other) could
+      # run at CT, expecting c semantics.
+      discard
 
 static: main()
 main()
diff --git a/tests/stdlib/tstring.nim b/tests/stdlib/tstring.nim
index 4d5a15940..fcbacc533 100644
--- a/tests/stdlib/tstring.nim
+++ b/tests/stdlib/tstring.nim
@@ -96,11 +96,43 @@ func reverse*(a: string): string =
 
 
 proc main() =
+  # xxx put all tests here to test in VM and RT
   test_string_slice()
   test_string_cmp()
 
   tester(1)
-  doAssert reverse("hello") == "olleh"
+
+  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()