Skip to content

Commit

Permalink
add issue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 18, 2024
1 parent 770dc59 commit 2300021
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/sandwich/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
switch("experimental", "typeBoundOps")
15 changes: 15 additions & 0 deletions tests/sandwich/mcontext_thread_local.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# context_thread_local
import ./mtasks, ./mlistdeques

export mlistdeques # Exporting the type with destructor doesn't help
# export tasks # solution 1. Exporting the inner type

type MagicCompile = object
dq: ListDeque[Task]

# var x: MagicCompile # solution 2. Instantiating the type with destructors
echo "Success"

type
TLContext* = object
deque*: ListDeque[Task]
11 changes: 11 additions & 0 deletions tests/sandwich/mfiles.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import mhandles

type
File* = ref object
handle: Handle[FD]

proc close*[T: File](f: T) =
f.handle.close()

proc newFile*(fd: FD): File =
File(handle: initHandle(FD -1))
19 changes: 19 additions & 0 deletions tests/sandwich/mhandles.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type
FD* = distinct cint

type
AnyFD* = concept fd
close(fd)

proc close*(fd: FD) =
discard

type
Handle*[T: AnyFD] = object
fd: T

proc close*[T: AnyFD](h: var Handle[T]) =
close h.fd

proc initHandle*[T: AnyFD](fd: T): Handle[T] =
Handle[T](fd: fd)
52 changes: 52 additions & 0 deletions tests/sandwich/mitems.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import sets, hashes

type
Fruit* = ref object
id*: int

# Generic implementation. This doesn't work
EntGroup*[T] = ref object
freed*: HashSet[T]

proc hash*(self: Fruit): Hash = hash(self.id)

##
## VVV The Generic implementation. This doesn't work VVV
##

proc initEntGroup*[T: Fruit](): EntGroup[T] =
result = EntGroup[T]()
result.freed = initHashSet[Fruit]()
var apple = Fruit(id: 20)
result.freed.incl(apple)

proc get*[T: Fruit](fg: EntGroup[T]): T =
if len(fg.freed) == 0: return
# vvv It errors here
# type mismatch: ([1] fg.freed: HashSet[grouptest.Fruit])
for it in fg.freed:
return it

##
## VVV The Non-Generic implementation works VVV
##
type
# Non-generic implementation. This works.
FruitGroup* = ref object
freed*: HashSet[Fruit]

proc initFruitGroup*(): FruitGroup =
result = FruitGroup()
result.freed = initHashSet[Fruit]()
var apple = Fruit(id: 20)
result.freed.incl(apple)

proc getNoGeneric*(fg: FruitGroup): Fruit =
if len(fg.freed) == 0: return
for it in fg.freed:
return it

proc `$`*(self: Fruit): string =
# For echo
if self == nil: return "Fruit()"
return "Fruit(" & $(self.id) & ")"
35 changes: 35 additions & 0 deletions tests/sandwich/mlistdeques.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# listdeques

# needed, type bound ops aren't considered for undeclared procs
type Placeholder = object
proc allocate(_: Placeholder) = discard
proc delete(_: Placeholder) = discard

type
StealableTask* = concept task, var mutTask, type T
task is ptr
task.prev is T
task.next is T
task.parent is T
task.fn is proc (param: pointer) {.nimcall.}
allocate(mutTask)
delete(task)

ListDeque*[T: StealableTask] = object
head, tail: T

func isEmpty*(dq: ListDeque): bool {.inline.} =
discard

func popFirst*[T](dq: var ListDeque[T]): T =
discard

proc `=destroy`*[T: StealableTask](dq: var ListDeque[T]) =
mixin delete
if dq.isEmpty():
return

while (let task = dq.popFirst(); not task.isNil):
delete(task)

delete(dq.head)
11 changes: 11 additions & 0 deletions tests/sandwich/mobjhash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ proc `==`*(a, b: GenericObj4): bool =

proc hash*(a: GenericObj4): Hash =
!$(hash(a.x) !& hash(a.y))

type
GenericRefObj*[T] = ref object
x*, y*: T
z*: string # to be ignored for equality

proc `==`*[T](a, b: GenericRefObj[T]): bool =
a.x == b.x and a.y == b.y

proc hash*[T](a: GenericRefObj[T]): Hash =
!$(hash(a.x) !& hash(a.y))
13 changes: 13 additions & 0 deletions tests/sandwich/mqueuecontainer.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# original example used queues
import deques

type
QueueContainer*[T] = object
q: ref Deque[T]

proc init*[T](c: var QueueContainer[T]) =
new(c.q)
c.q[] = initDeque[T](64)

proc addToQ*[T](c: var QueueContainer[T], item: T) =
c.q[].addLast(item)
10 changes: 10 additions & 0 deletions tests/sandwich/msetin.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import std/sets
template foo*[T](a: T) =
# proc foo*[T](a: T) = # works
var s: HashSet[T]
# echo contains(s, a) # works
let x = a in s # BUG
doAssert not x
doAssert not (a in s)
doAssert a notin s
when isMainModule: foo(1) # works
7 changes: 7 additions & 0 deletions tests/sandwich/msetiter1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sets

proc initH*[V]: HashSet[V] =
result = initHashSet[V]()

proc foo*[V](h: var HashSet[V], c: seq[V]) =
h = h + c.toHashSet()
4 changes: 4 additions & 0 deletions tests/sandwich/msetiter2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sets, sequtils

proc dedupe*[T](arr: openArray[T]): seq[T] =
arr.toHashSet.toSeq
14 changes: 14 additions & 0 deletions tests/sandwich/mtasks.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# tasks.nim
type
Task* = ptr object
parent*: Task
prev*: Task
next*: Task
fn*: proc (param: pointer) {.nimcall.}

# StealableTask API
proc allocate*(task: var Task) =
discard

proc delete*(task: Task) =
discard
12 changes: 12 additions & 0 deletions tests/sandwich/tcontext_thread_local.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
discard """
output: '''
Success
'''
"""

# modified issue #12620, see placeholder procs in mlistdeques

# runtime.nim
import ./mcontext_thread_local

var localCtx* : TLContext
8 changes: 8 additions & 0 deletions tests/sandwich/tfilehandles.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# issue #16755

import mfiles
from mhandles import FD
#import handles <- do this and it works

let wr = newFile(FD -1)
close wr
13 changes: 13 additions & 0 deletions tests/sandwich/titems.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# issue #22984
# import sets # <<-- Uncomment this to make the error go away

import mitems

## The generic implementation
var grp: EntGroup[Fruit] = initEntGroup[Fruit]()
doAssert $get(grp) == "Fruit(20)" ## Errors here


## This works though (Non-generic)
var fruitGroup: FruitGroup = initFruitGroup()
doAssert $getNoGeneric(fruitGroup) == "Fruit(20)"
15 changes: 14 additions & 1 deletion tests/sandwich/tobjhash.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://github.com/nim-lang/RFCs/issues/380

from mobjhash import Obj, RefObj, GenericObj1, GenericObj2, GenericObj3, GenericObj4
from mobjhash import Obj, RefObj, GenericObj1, GenericObj2, GenericObj3, GenericObj4, GenericRefObj
import tables

block:
Expand Down Expand Up @@ -44,3 +44,16 @@ block:
t[GenericObj4[float](x: 3, y: 4, z: "debug")] = 34
doAssert t[GenericObj4[float](x: 3, y: 4, z: "ignored")] == 34
doAssert GenericObj4[float](x: 4, y: 3, z: "debug") notin t

block:
var t: Table[GenericRefObj[float], int]
t[GenericRefObj[float](x: 3, y: 4, z: "debug")] = 34
doAssert t[GenericRefObj[float](x: 3, y: 4, z: "ignored")] == 34
doAssert GenericRefObj[float](x: 4, y: 3, z: "debug") notin t

block:
type LocalAlias[T] = GenericObj4[T]
var t: Table[LocalAlias[float], int]
t[LocalAlias[float](x: 3, y: 4, z: "debug")] = 34
doAssert t[LocalAlias[float](x: 3, y: 4, z: "ignored")] == 34
doAssert LocalAlias[float](x: 4, y: 3, z: "debug") notin t
10 changes: 10 additions & 0 deletions tests/sandwich/tqueuecontainer.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# issue #4773

import mqueuecontainer

# works if this is uncommented (or if the `queuecontainer` exports `queues`):
# import queues

var c: QueueContainer[int]
c.init()
c.addToQ(1)
24 changes: 24 additions & 0 deletions tests/sandwich/tsethash.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# issue #14729

import sets, hashes

type
Iterable[T] = concept x
for value in items(x):
type(value) is T

Foo[T] = object
t: T

proc myToSet[T](keys: Iterable[T]): HashSet[T] =
for x in items(keys): result.incl(x)

proc hash[T](foo: Foo[T]): Hash =
echo "specific hash"

proc `==`[T](lhs, rhs: Foo[T]): bool =
echo "specific equals"

let
f = Foo[string](t: "test")
hs = [f, f].myToSet()
4 changes: 4 additions & 0 deletions tests/sandwich/tsetin.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# issue #18150

import msetin
foo(1)
17 changes: 17 additions & 0 deletions tests/sandwich/tsetiter1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# comment on issue #11167

import hashes

import msetiter

type
Choice = object
i: int

proc hash(c: Choice): Hash =
result = Hash(c.i)

var h = initH[Choice]()
let c = @[Choice(i: 1)]

foo(h, c)
9 changes: 9 additions & 0 deletions tests/sandwich/tsetiter2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# comment on issue #11167

import msetiter2

let x = dedupe([1, 2, 3])
doAssert x.len == 3
doAssert 1 in x
doAssert 2 in x
doAssert 3 in x

0 comments on commit 2300021

Please sign in to comment.