ttlsemulation.nim 2.0 KB

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