From 5e17ffae3ab13a00025ed1a7f4dda6ddde967741 Mon Sep 17 00:00:00 2001 From: John Dupuy Date: Thu, 14 May 2020 17:44:39 -0500 Subject: [PATCH] Fixed output for writeText(s,float) and added unit test --- faststreams/textio.nim | 10 ++++------ tests/test_outputs.nim | 12 ++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/faststreams/textio.nim b/faststreams/textio.nim index 0fdf35b..8a138c8 100644 --- a/faststreams/textio.nim +++ b/faststreams/textio.nim @@ -99,15 +99,13 @@ when defined(c): ## than `$` because it is directly written to the stream without ## allocating a standard Nim 'string'. ## - ## Because this procedure uses stdlib, it is only active when nim compiles - ## with the 'c' flag. Otherwise, float types fall back to the generic writeText + ## Because this procedure uses stdlib, it is only active when Nim compiles + ## with the 'c' flag. Otherwise, float types are passed to the generic writeText ## procedure. var buffer: array[65, char] discard writeFloatToBuffer(buffer, x) - for index in 0 ..< 65: - if buffer[index] == '\0': - break - write s, buffer[index] + let bs: cstring = addr buffer + write s, bs.toOpenArray(0, bs.len - 1) template writeText*(s: OutputStream, str: string) = write s, str diff --git a/tests/test_outputs.nim b/tests/test_outputs.nim index 8426607..dc461bb 100644 --- a/tests/test_outputs.nim +++ b/tests/test_outputs.nim @@ -236,3 +236,15 @@ suite "output stream": let resultsAreEqual = nimSeq == memStream.getOutput check resultsAreEqual + test "float output": + let basic: float64 = 12345.125 + let small: float32 = 12345.125 + let large: float64 = 9.99e+20 + let tiny: float64 = -2.25e-35 + + outputText basic + outputText small + outputText large + outputText tiny + + checkOutputsMatch()