summary refs log tree commit diff stats
path: root/tests/misc/ttlsemulation.nim
blob: 47c5934e66ad9c6ba91268ae1f7683a5aa987969 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
discard """
  matrix: "-d:nimTtlsemulationCase1 --threads --tlsEmulation:on; -d:nimTtlsemulationCase2 --threads --tlsEmulation:off; -d:nimTtlsemulationCase3 --threads"
  targets: "c cpp"
"""

#[
tests for: `.cppNonPod`, `--tlsEmulation`
]#

import std/sugar

block:
  # makes sure the logic in config/nim.cfg or testament doesn't interfere with `--tlsEmulation` so we test the right thing.
  when defined(nimTtlsemulationCase1):
    doAssert compileOption("tlsEmulation")
  elif defined(nimTtlsemulationCase2):
    doAssert not compileOption("tlsEmulation")
  elif defined(nimTtlsemulationCase3):
    when defined(osx):
      doAssert not compileOption("tlsEmulation")
  else:
    doAssert false

block:
  proc main1(): int =
    var g0 {.threadvar.}: int
    g0.inc
    g0
  let s = collect:
    for i in 0..<3: main1()
  doAssert s == @[1,2,3]

when defined(cpp): # bug #16752
  when defined(windows) and defined(nimTtlsemulationCase2):
    discard # xxx this failed with exitCode 1
  else:
    type Foo1 {.importcpp: "Foo1", header: "mtlsemulation.h".} = object
      x: cint
    type Foo2 {.cppNonPod, importcpp: "Foo2", header: "mtlsemulation.h".} = object
      x: cint

    var ctorCalls {.importcpp.}: cint
    var dtorCalls {.importcpp.}: cint
    type Foo3 {.cppNonPod, importcpp: "Foo3", header: "mtlsemulation.h".} = object
      x: cint

    proc sub(i: int) =
      var g1 {.threadvar.}: Foo1
      var g2 {.threadvar.}: Foo2
      var g3 {.threadvar.}: Foo3
      discard g1
      discard g2

      # echo (g3.x, ctorCalls, dtorCalls)
      when compileOption("tlsEmulation"):
        # xxx bug
        discard
      else:
        doAssert g3.x.int == 10 + i
        doAssert ctorCalls == 2
      doAssert dtorCalls == 1
      g3.x.inc

    proc main() =
      doAssert ctorCalls == 0
      doAssert dtorCalls == 0
      block:
        var f3: Foo3
        doAssert f3.x == 10
      doAssert ctorCalls == 1
      doAssert dtorCalls == 1

      for i in 0..<3:
        sub(i)
    main()