summary refs log tree commit diff stats
path: root/tests/objects/tobject_default_value.nim
blob: b571965ea13b2810accf09a65e84afd53b5becab (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
discard """
  matrix: "-d:nimPreviewRangeDefault --mm:refc; -d:nimPreviewRangeDefault --warningAsError:ProveInit --mm:orc"
  targets: "c cpp js"
"""

import std/[times, macros, tables]

type
  Guess = object
    poi: DateTime

  GuessDistinct = distinct Guess

block:
  var x: Guess
  discard Guess()

  var y: GuessDistinct

  discard y

  discard GuessDistinct(x)


import mobject_default_value

block:
  let x = Default()
  doAssert x.se == 0'i32

block:
  let x = default(Default)
  doAssert x.se == 0'i32
# echo Default(poi: 12)
# echo Default(poi: 17)

# echo NonDefault(poi: 77)

block:
  var x: Default
  doAssert x.se == 0'i32

type
  ObjectBase = object of RootObj
    value = 12

  ObjectBaseDistinct = distinct ObjectBase

  DinstinctInObject = object
    data: ObjectBaseDistinct

  Object = object of ObjectBase
    time: float = 1.2
    date: int
    scale: range[1..10]

  Object2 = object
    name: Object

  Object3 = object
    obj: Object2

  ObjectTuple = tuple
    base: ObjectBase
    typ: int
    obj: Object

  TupleInObject = object
    size = 777
    data: ObjectTuple

  Ref = ref object of ObjectBase

  RefInt = ref object of Ref
    data = 73

  Ref2 = ref object of ObjectBase

  RefInt2 = ref object of Ref
    data = 73

var t {.threadvar.}: Default
# var m1, m2 {.threadvar.}: Default

block:
  doAssert t.se == 0'i32

block: # ARC/ORC cannot bind destructors twice, so it cannot
      # be moved into main
  block:
    var x: Ref
    new(x)
    doAssert x.value == 12, "Ref.value = " & $x.value

    var y: RefInt
    new(y)
    doAssert y.value == 12
    doAssert y.data == 73

  block:
    var x: Ref2
    new(x, proc (x: Ref2) {.nimcall.} = discard "call Ref")
    doAssert x.value == 12, "Ref.value = " & $x.value

    proc call(x: RefInt2) =
      discard "call RefInt"

    var y: RefInt2
    new(y, call)
    doAssert y.value == 12
    doAssert y.data == 73

template main {.dirty.} =
  block: # bug #16744
    type
      R = range[1..10]
      Obj = object
        r: R

    var
      rVal: R = default(R) # Works fine
      objVal = default(Obj)

    doAssert rVal == 0 # it should be 1
    doAssert objVal.r == 1

  block: # bug #16744
    type
      R = range[1..10]
      Obj = object
        r: R

    var
      rVal: R = default(R) # Works fine
      objVal = Obj()

    doAssert rVal == 0 # it should be 1
    doAssert objVal.r == 1

  block: # bug #3608
    type
      abc = ref object
        w: range[2..100]

    proc createABC(): abc =
      new(result)
      result.w = 20

    doAssert createABC().w == 20

  block:
    var x = new ObjectBase
    doAssert x.value == 12

    proc hello(): ref ObjectBase =
      new result

    let z = hello()
    doAssert z.value == 12

  block:
    var base = ObjectBase()
    var x: ObjectBaseDistinct = ObjectBaseDistinct(base)
    doAssert ObjectBase(x).value == 12
    let y = ObjectBaseDistinct(default(ObjectBase))
    doAssert ObjectBase(y).value == 12

    let m = ObjectBaseDistinct(ObjectBase())
    doAssert ObjectBase(m).value == 12

    proc hello(): ObjectBaseDistinct =
      result = ObjectBaseDistinct(default(ObjectBase))

    let z = hello()
    doAssert ObjectBase(z).value == 12

  block:
    var x: DinstinctInObject
    x.data = ObjectBaseDistinct(default(ObjectBase))

    doAssert ObjectBase(x.data).value == 12

  block:
    var x = Object()
    doAssert x.value == 12
    doAssert x.time == 1.2
    doAssert x.scale == 1

    let y = default(Object)
    doAssert y.value == 12
    doAssert y.time == 1.2
    doAssert y.scale == 1

    var x1, x2, x3 = default(Object)
    doAssert x1.value == 12
    doAssert x1.time == 1.2
    doAssert x1.scale == 1
    doAssert x2.value == 12
    doAssert x2.time == 1.2
    doAssert x2.scale == 1
    doAssert x3.value == 12
    doAssert x3.time == 1.2
    doAssert x3.scale == 1

  block:
    var x = new Object
    doAssert x[] == default(Object)

  block:
    var x = default(Object2)
    doAssert x.name.value == 12
    doAssert x.name.time == 1.2
    doAssert x.name.scale == 1

  block:
    let x = Object2()
    doAssert x.name.value == 12
    doAssert x.name.time == 1.2
    doAssert x.name.scale == 1

  block:
    var x: ref Object2
    new x
    doAssert x[] == default(Object2)

  block:
    var x = default(Object3)
    doAssert x.obj.name.value == 12
    doAssert x.obj.name.time == 1.2
    doAssert x.obj.name.scale == 1

  block:
    var x = Object3()
    doAssert x.obj.name.value == 12
    doAssert x.obj.name.time == 1.2
    doAssert x.obj.name.scale == 1

  when nimvm:
    # todo
    discard "fixme"
  else:
    when defined(gcArc) or defined(gcOrc):
      block: #seq
        var x = newSeq[Object](10)
        let y = x[0]
        doAssert y.value == 12
        doAssert y.time == 1.2
        doAssert y.scale == 1

      block:
        var x: seq[Object]
        setLen(x, 5)
        let y = x[^1]
        doAssert y.value == 12
        doAssert y.time == 1.2
        doAssert y.scale == 1

      block:
        var my = @[1, 2, 3, 4, 5]
        my.setLen(0)
        my.setLen(5)
        doAssert my == @[0, 0, 0, 0, 0]

      block:
        var my = "hello"
        my.setLen(0)
        my.setLen(5)
        doAssert $(@my) == """@['\x00', '\x00', '\x00', '\x00', '\x00']"""

  block: # array
    var x: array[10, Object] = default(array[10, Object])
    let y = x[0]
    doAssert y.value == 12
    doAssert y.time == 1.2
    doAssert y.scale == 1

  block: # array
    var x {.noinit.}: array[10, Object]
    discard x

  block: # tuple
    var x = default(ObjectTuple)
    doAssert x.base.value == 12
    doAssert x.typ == 0
    doAssert x.obj.time == 1.2
    doAssert x.obj.date == 0
    doAssert x.obj.scale == 1
    doAssert x.obj.value == 12

  block: # tuple in object
    var x = default(TupleInObject)
    doAssert x.data.base.value == 12
    doAssert x.data.typ == 0
    doAssert x.data.obj.time == 1.2
    doAssert x.data.obj.date == 0
    doAssert x.data.obj.scale == 1
    doAssert x.data.obj.value == 12
    doAssert x.size == 777

  type
    ObjectArray = object
      data: array[10, Object]

  block:
    var x = default(ObjectArray)
    let y = x.data[0]
    doAssert y.value == 12
    doAssert y.time == 1.2
    doAssert y.scale == 1

  block:
    var x: PrellDeque[int]
    doAssert x.pendingTasks == 0

  type
    Color = enum
      Red, Blue, Yellow

    ObjectVarint = object
      case kind: Color
      of Red:
        data: int = 10
      of Blue:
        fill = "123"
      of Yellow:
        time = 1.8'f32

    ObjectVarint1 = object
      case kind: Color = Blue
      of Red:
        data1: int = 10
      of Blue:
        fill2 = "123"
        cry: float
      of Yellow:
        time3 = 1.8'f32
        him: int

  block:
    var x = ObjectVarint(kind: Red)
    doAssert x.kind == Red
    doAssert x.data == 10

  block:
    var x = ObjectVarint(kind: Blue)
    doAssert x.kind == Blue
    doAssert x.fill == "123"

  block:
    var x = ObjectVarint(kind: Yellow)
    doAssert x.kind == Yellow
    doAssert typeof(x.time) is float32

  block:
    var x = default(ObjectVarint1)
    doAssert x.kind == Blue
    doAssert x.fill2 == "123"
    x.cry = 326

  type
    ObjectVarint2 = object
      case kind: Color
      of Red:
        data: int = 10
      of Blue:
        fill = "123"
      of Yellow:
        time = 1.8'f32

  block:
    var x = ObjectVarint2(kind: Blue)
    doAssert x.fill == "123"

  block:
    type
      Color = enum
        Red, Blue, Yellow
  
    type
      ObjectVarint3 = object # fixme it doesn't work with static
        case kind: Color = Blue
        of Red:
          data1: int = 10
        of Blue:
          case name: Color = Blue
          of Blue:
            go = 12
          else:
            temp = 66
          fill2 = "123"
          cry: float
        of Yellow:
          time3 = 1.8'f32
          him: int

    block:
      var x = default(ObjectVarint3)
      doAssert x.kind == Blue
      doAssert x.name == Blue
      doAssert x.go == 12

    block:
      var x = ObjectVarint3(kind: Blue, name: Red, temp: 99)
      doAssert x.kind == Blue
      doAssert x.name == Red
      doAssert x.temp == 99

  block:
    type
      Default = tuple
        id: int = 1
        obj: ObjectBase
        name: string

      Class = object
        def: Default

      Member = object
        def: Default = (id: 777, obj: ObjectBase(), name: "fine")

    block:
      var x = default(Default)
      doAssert x.id == 1
      doAssert x.obj == default(ObjectBase)
      doAssert x.name == ""

    block:
      var x = default(Class)
      doAssert x.def == default(Default)
      doAssert x.def.id == 1
      doAssert x.def.obj == default(ObjectBase)
      doAssert x.def.name == ""

    block:
      var x = default(Member)
      doAssert x.def.id == 777
      doAssert x.def.obj == default(ObjectBase)
      doAssert x.def.name == "fine"

  block:
    var x {.noinit.} = 12
    doAssert x == 12

    type
      Pure = object
        id: int = 12

    var y {.noinit.}: Pure
    doAssert y.id == 0

    var z {.noinit.}: Pure = Pure(id: 77)
    doAssert z.id == 77

  block: # bug #20681
    type A = object
      d: DateTime = DateTime()

    let x = default(A)
    doAssert $x == "(d: Uninitialized DateTime)"

  block: # bug #20715
    block:
      type
        Foo = enum
          A
          B

        Bar = object
          case foo: Foo
          of A:
            t: range[-1..2]
          else: discard

      var d = default(Bar)
      doAssert d.t == -1

    block:
      type
        Foo = enum
          A
          B

        Bar = object
          case foo: Foo
          of A:
            t: range[0..2]
          else: discard

      var d = default(Bar)
      doAssert d.t == 0

    block: # bug #20740
      block:
        proc foo(x: static DateTime = Datetime()) =
          discard

        foo()

      block:
        macro foo(x: static DateTime) =
          discard x

        macro foo2: untyped =
          var x = DateTime()

          result = quote do:
            foo(`x`)

        foo2()


  block: # issue #20699
    type
      Either[A,B] = object
        case kind:bool
        of false:
          b: B
        of true:
            a: A
      O = object of RootRef

    proc oToEither(o:O):Either[O,void] =
      Either[O,void](kind:true,a: o)

    discard oToEither(O())

  block: # bug #20695
    type
      Default = object
        tabs: Table[string, int] = initTable[string, int]()

    let d = default(Default)
    doAssert d.tabs.len == 0

  block:
    type
      Default = object
        tabs: Table[string, int] = Table[string, int]()

    let d = default(Default)
    doAssert d.tabs.len == 0


  block:
    type DjangoDateTime = distinct DateTime

    type Default = object
      data: DjangoDateTime = DjangoDateTime(DateTime())

    let x = default(Default)
    doAssert x.data is DjangoDateTime

  block:
    type DjangoDateTime = distinct DateTime

    type Default = object
      data = DjangoDateTime(DateTime())

    let x = default(Default)
    doAssert x.data is DjangoDateTime

  block:
    type
      Result2 = object
        case o: bool
        of false:
          e: float
        of true:
          v {.requiresInit.} : int = 1

    proc startSessionSync(): Result2 =
      return Result2(o: true)

    proc mainSync =
      let ff = startSessionSync()
      doAssert ff.v == 1

    mainSync()

  block:
    type
      Result2 = object
        v {.requiresInit.} : int = 1

    proc startSessionSync(): Result2 =
      return Result2()

    proc mainSync =
      let ff = startSessionSync()
      doAssert ff.v == 1

    mainSync()

  block: # bug #21801
    func evaluate(i: int): float =
      0.0

    func evaluate(): float =
      0.0

    type SearchOptions = object
        evaluation: proc(): float = evaluate

  block:
    func evaluate(): float =
      0.0

    type SearchOptions = object
        evaluation: proc(): float = evaluate

  block:
    func evaluate(i: int): float =
      0.0

    type SearchOptions = object
        evaluation = evaluate
  block:
    type
      Result[T, E] = object
        when T is void:
          when E is void:
            oResultPrivate: bool
          else:
            case oResultPrivate: bool
            of false:
              eResultPrivate: E
            of true:
              discard
        else:
          when E is void:
            case oResultPrivate: bool
            of false:
              discard
            of true:
              vResultPrivate: T
          else:
            case oResultPrivate: bool
            of false:
              eResultPrivate: E
            of true:
              vResultPrivate: T


    template `?`[T, E](self: Result[T, E]): auto =
      let v = (self)
      if not v.oResultPrivate:
        when compiles(`assignResult?`(default(typeof(result)))):
          when typeof(result) is typeof(v):
            `assignResult?`(v)
          elif E is void:
            `assignResult?`(err(typeof(result)))
          else:
            `assignResult?`(err(typeof(result), v.eResultPrivate))
          return
        else:
          return
            when typeof(result) is typeof(v):
              v
            elif E is void:
              err(typeof(result))
            else:
              err(typeof(result), v.eResultPrivate)

      when not(T is void):
        v.vResultPrivate
        
    type R = Result[int, string]

    proc testAssignResult() =
      var assigned: bool
      template `assignResult?`(v: Result) =
        assigned = true
        result = v

      proc failed(): Result[int, string] =
        discard

      proc calling(): Result[int, string] =
        let _ = ? failed()
        doAssert false

      let r = calling()
      doAssert assigned

    when nimvm:
      when not defined(js):
        testAssignResult()
    else:
      testAssignResult()


static: main()
main()