Skip to content

Commit

Permalink
fix test: incorrect printf format specifier discovered by this featur…
Browse files Browse the repository at this point in the history
…e; simplify test now that nim-lang#13489 was merged
  • Loading branch information
timotheecour committed Mar 10, 2020
1 parent 002001f commit fc8dc00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
30 changes: 14 additions & 16 deletions tests/vm/mevalffi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,32 @@ proc fun() =
doAssert c_exp(x2) == c_exp(x)

block: # c_printf
c_printf("foo\n")
c_printf("foo:%d\n", 100)
c_printf("foo:%d\n", 101.cint)
c_printf("foo:%d:%d\n", 102.cint, 103.cint)
c_printf("foo0\n")
c_printf("foo1:%d\n", 101.cint)
c_printf("foo2:%d:%d\n", 102.cint, 103.cint)
let temp = 104.cint
c_printf("foo:%d:%d:%d\n", 102.cint, 103.cint, temp)
c_printf("foo3:%d:%d:%d\n", 102.cint, 103.cint, temp)
var temp2 = 105.cint
c_printf("foo:%g:%s:%d:%d\n", 0.03, "asdf", 103.cint, temp2)
c_printf("foo4:%g:%s:%d:%d\n", 0.03, "asdf", 103.cint, temp2)

block: # c_snprintf, c_malloc, c_free
let n: uint = 50
var buffer2: pointer = c_malloc(n)
var s: cstring = "foobar"
var age: cint = 25
discard c_snprintf(buffer2, n, "s1:%s s2:%s age:%d pi:%g", s, s, age, 3.14)
c_printf("ret={%s}\n", buffer2)
c_printf("foo5:{%s}\n", buffer2)
c_free(buffer2) # not sure it has an effect

block: # c_printf bug
var a = 123
block: # c_printf bug with `var` {.varargs.} params
var a = 123.cint
var a2 = a.addr
#[
bug: different behavior between CT RT in this case:
at CT, shows foo2:a=123
at RT, shows foo2:a=<address as int>
]#
if false:
c_printf("foo2:a=%d\n", a2)
when false:
# BUG: CT FFI currently does not handle this edge case correctly,
# passing `a2` as `cint` instead of as `ptr cint` for a {.varargs.} param:
# at CT, shows foo6:a2=0x7b
# at RT, shows foo2:a2=<address>
c_printf("foo6:a2=%p\n", a2)


static:
Expand Down
14 changes: 6 additions & 8 deletions tests/vm/tevalffi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import std/[strformat,os,osproc]
proc main() =
const nim = getCurrentCompilerExe()
const file = currentSourcePath().parentDir / "mevalffi.nim"
# strangely, --hint:cc:off was needed
let cmd = fmt"{nim} c -f --experimental:compiletimeFFI --hints:off --hint:cc:off {file}"
let (output, exitCode) = execCmdEx(cmd)
let expected = """
hello world stderr
hi stderr
foo
foo:100
foo:101
foo:102:103
foo:102:103:104
foo:0.03:asdf:103:105
ret={s1:foobar s2:foobar age:25 pi:3.14}
foo0
foo1:101
foo2:102:103
foo3:102:103:104
foo4:0.03:asdf:103:105
foo5:{s1:foobar s2:foobar age:25 pi:3.14}
"""
doAssert output == expected, output
doAssert exitCode == 0
Expand Down

0 comments on commit fc8dc00

Please sign in to comment.