ttlsemulation.nim 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. discard """
  2. disabled: i386
  3. matrix: "-d:nimTtlsemulationCase1 --threads --tlsEmulation:on; -d:nimTtlsemulationCase2 --threads --tlsEmulation:off; -d:nimTtlsemulationCase3 --threads"
  4. targets: "c cpp"
  5. """
  6. #[
  7. tests for: `.cppNonPod`, `--tlsEmulation`
  8. ]#
  9. import std/sugar
  10. block:
  11. # makes sure the logic in config/nim.cfg or testament doesn't interfere with `--tlsEmulation` so we test the right thing.
  12. when defined(nimTtlsemulationCase1):
  13. doAssert compileOption("tlsEmulation")
  14. elif defined(nimTtlsemulationCase2):
  15. doAssert not compileOption("tlsEmulation")
  16. elif defined(nimTtlsemulationCase3):
  17. when defined(osx):
  18. doAssert not compileOption("tlsEmulation")
  19. else:
  20. doAssert false
  21. block:
  22. proc main1(): int =
  23. var g0 {.threadvar.}: int
  24. g0.inc
  25. g0
  26. let s = collect:
  27. for i in 0..<3: main1()
  28. doAssert s == @[1,2,3]
  29. when defined(cpp): # bug #16752
  30. when defined(windows) and defined(nimTtlsemulationCase2):
  31. discard # xxx this failed with exitCode 1
  32. else:
  33. type Foo1 {.importcpp: "Foo1", header: "mtlsemulation.h".} = object
  34. x: cint
  35. type Foo2 {.cppNonPod, importcpp: "Foo2", header: "mtlsemulation.h".} = object
  36. x: cint
  37. var ctorCalls {.importcpp.}: cint
  38. var dtorCalls {.importcpp.}: cint
  39. type Foo3 {.cppNonPod, importcpp: "Foo3", header: "mtlsemulation.h".} = object
  40. x: cint
  41. proc sub(i: int) =
  42. var g1 {.threadvar.}: Foo1
  43. var g2 {.threadvar.}: Foo2
  44. var g3 {.threadvar.}: Foo3
  45. discard g1
  46. discard g2
  47. # echo (g3.x, ctorCalls, dtorCalls)
  48. when compileOption("tlsEmulation"):
  49. # xxx bug
  50. discard
  51. else:
  52. doAssert g3.x.int == 10 + i
  53. doAssert ctorCalls == 2
  54. doAssert dtorCalls == 1
  55. g3.x.inc
  56. proc main() =
  57. doAssert ctorCalls == 0
  58. doAssert dtorCalls == 0
  59. block:
  60. var f3: Foo3
  61. doAssert f3.x == 10
  62. doAssert ctorCalls == 1
  63. doAssert dtorCalls == 1
  64. for i in 0..<3:
  65. sub(i)
  66. main()