some test cleanups & category reorganization (#22010)

* clean up some test categories

* mention exact slice issue

* magics into system

* move trangechecks into overflow

* move tmemory to system

* try fix CI

* try fix CI

* final CI fix
This commit is contained in:
metagn
2023-06-06 07:54:07 +03:00
committed by GitHub
parent 2ab948ce53
commit b97d603cd0
134 changed files with 189 additions and 374 deletions

24
tests/system/t10307.nim Normal file
View File

@@ -0,0 +1,24 @@
discard """
cmd: "nim c --mm:refc -d:useGcAssert $file"
output: '''running someProc(true)
res: yes
yes
running someProc(false)
res:
'''
"""
proc someProc(x:bool):cstring =
var res:string = ""
if x:
res = "yes"
echo "res: ", res
GC_ref(res)
result = res
echo "running someProc(true)"
echo someProc(true)
echo "running someProc(false)"
echo someProc(false)

12
tests/system/t20938.nim Normal file
View File

@@ -0,0 +1,12 @@
discard """
cmd: "nim c --mm:refc $file"
action: "compile"
"""
template foo(x: typed) =
discard x
foo:
var x = "hello"
x.shallowCopy("test")
true

11
tests/system/tconcat.nim Normal file
View File

@@ -0,0 +1,11 @@
discard """
output: "DabcD"
"""
const
x = "abc"
var v = "D" & x & "D"
echo v

View File

@@ -0,0 +1,6 @@
discard """
output: "\n"
"""
echo()

View File

@@ -0,0 +1,112 @@
discard """
output: '''
a char: true
a char: false
an int: 5
an int: 6
a string: abc
false
true
true
false
true
a: a
b: b
x: 5
y: 6
z: abc
a char: true
a char: false
an int: 5
an int: 6
a string: abc
a string: I'm root!
CMP false
CMP true
CMP true
CMP false
CMP true
CMP true
a: a
b: b
x: 5
y: 6
z: abc
thaRootMan: I'm root!
myDisc: enC
c: Z
enC
Z
'''
"""
block titerator1:
type
TMyTuple = tuple[a, b: char, x, y: int, z: string]
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
var x: TMyTuple = ('a', 'b', 5, 6, "abc")
var y: TMyTuple = ('A', 'b', 5, 9, "abc")
for f in fields(x):
p f
for a, b in fields(x, y):
echo a == b
for key, val in fieldPairs(x):
echo key, ": ", val
doAssert x != y
doAssert x == x
doAssert(not (x < x))
doAssert x <= x
doAssert y < x
doAssert y <= x
block titerator2:
type
SomeRootObj = object of RootObj
thaRootMan: string
TMyObj = object of SomeRootObj
a, b: char
x, y: int
z: string
TEnum = enum enA, enB, enC
TMyCaseObj = object
case myDisc: TEnum
of enA: a: int
of enB: b: string
of enC: c: char
proc p(x: char) = echo "a char: ", x <= 'a'
proc p(x: int) = echo "an int: ", x
proc p(x: string) = echo "a string: ", x
proc myobj(a, b: char, x, y: int, z: string): TMyObj =
result.a = a; result.b = b; result.x = x; result.y = y; result.z = z
result.thaRootMan = "I'm root!"
var x = myobj('a', 'b', 5, 6, "abc")
var y = myobj('A', 'b', 5, 9, "abc")
for f in fields(x):
p f
for a, b in fields(x, y):
echo "CMP ", a == b
for key, val in fieldPairs(x):
echo key, ": ", val
var co = TMyCaseObj(myDisc: enC, c: 'Z')
for key, val in fieldPairs(co):
echo key, ": ", val
for val in fields(co):
echo val

108
tests/system/tfields.nim Normal file
View File

@@ -0,0 +1,108 @@
discard """
output: '''
n
n
(one: 1, two: 2, three: 3)
1
2
3
(one: 4, two: 5, three: 6)
4
(one: 7, two: 8, three: 9)
7
8
9
(foo: 38, other: "string here")
43
100
90
'''
"""
block tindex:
type
TMyTuple = tuple[a, b: int]
proc indexOf(t: typedesc, name: string): int =
## takes a tuple and looks for the field by name.
## returs index of that field.
var
d: t
i = 0
for n, x in fieldPairs(d):
if n == name: return i
i.inc
raise newException(ValueError, "No field " & name & " in type " &
astToStr(t))
doAssert TMyTuple.indexOf("b") == 1
block ttemplate:
# bug #1902
# This works.
for name, value in (n: "v").fieldPairs:
echo name
template wrapper(): void =
for name, value in (n: "v").fieldPairs:
echo name
wrapper()
block tbreak:
# bug #2134
type
TestType = object
one: int
two: int
three: int
var
ab = TestType(one:1, two:2, three:3)
ac = TestType(one:4, two:5, three:6)
ad = TestType(one:7, two:8, three:9)
tstSeq = [ab, ac, ad]
for tstElement in mitems(tstSeq):
echo tstElement
for tstField in fields(tstElement):
#for tstField in [1,2,4,6]:
echo tstField
if tstField == 4:
break
block timplicit_with_partial:
type
Base = ref object of RootObj
Foo {.partial.} = ref object of Base
proc my(f: Foo) =
#var f.next = f
let f.foo = 38
let f.other = "string here"
echo f[]
echo f.foo + 5
var g: Foo
new(g)
my(g)
type
FooTask {.partial.} = ref object of RootObj
proc foo(t: FooTask) {.liftLocals: t.} =
var x = 90
if true:
var x = 10
while x < 100:
inc x
echo x
echo x
foo(FooTask())

View File

@@ -0,0 +1,6 @@
discard """
cmd: "nim c --gc:regions $file"
"""
# issue #12597
# it just tests that --gc:regions compiles. Nothing else. :'(

View File

@@ -0,0 +1,8 @@
discard """
errormsg: "type mismatch: got <int>"
file: "timmutableinc.nim"
line: 8
"""
var x = 0
inc(x+1)

View File

@@ -0,0 +1,19 @@
discard """
errormsg: "type mismatch"
file: "tinvalidnot.nim"
line: 14
"""
# BUG: following compiles, but should not:
proc nodeOfDegree(x: int): bool =
result = false
proc main =
for j in 0..2:
for i in 0..10:
if not nodeOfDegree(1) >= 0: #ERROR_MSG type mismatch
echo "Yes"
else:
echo "No"
main()

76
tests/system/tlocals.nim Normal file
View File

@@ -0,0 +1,76 @@
discard """
matrix: "--mm:refc; --mm:orc"
output: '''(x: "string here", a: 1)
b is 5
x is 12'''
"""
proc simple[T](a: T) =
var
x = "string here"
echo locals()
simple(1)
type Foo2[T]=object
a2: T
proc numFields*(T: typedesc[tuple|object]): int=
var t:T
for _ in t.fields: inc result
proc test(baz: int, qux: var int): int =
var foo: Foo2[int]
let bar = "abc"
let c1 = locals()
doAssert numFields(c1.foo.type) == 1
doAssert c1.bar == "abc"
doAssert c1.baz == 123
doAssert c1.result == 0
doAssert c1.qux == 456
var x1 = 456
discard test(123, x1)
# bug #11958
proc foo() =
var a = 5
proc bar() {.nimcall.} =
var b = 5
for k, v in fieldpairs(locals()):
echo k, " is ", v
bar()
foo()
proc foo2() =
var a = 5
proc bar2() {.nimcall.} =
for k, v in fieldpairs(locals()):
echo k, " is ", v
bar2()
foo2()
proc foo3[T](y: T) =
var a = 5
proc bar2[T](x: T) {.nimcall.} =
for k, v in fieldpairs(locals()):
echo k, " is ", v
bar2(y)
foo3(12)
block: # bug #12682
template foo(): untyped =
var c1 = locals()
1
proc testAll()=
doAssert foo() == 1
let c2=locals()
testAll()

32
tests/system/tlowhigh.nim Normal file
View File

@@ -0,0 +1,32 @@
discard """
action: run
output: '''
18446744073709551615
9223372036854775807
4294967295
0
0
'''
"""
var x: range[-1'f32..1'f32]
doAssert x.low == -1'f32
doAssert x.high == 1'f32
doAssert x.type.low == -1'f32
doAssert x.type.high == 1'f32
var y: range[-1'f64..1'f64]
doAssert y.low == -1'f64
doAssert y.high == 1'f64
doAssert y.type.low == -1'f64
doAssert y.type.high == 1'f64
# bug #11972
var num: uint8
doAssert num.high.float == 255.0
echo high(uint64)
echo high(int64)
echo high(uint32)
echo low(uint64)
echo low(uint32)

66
tests/system/tmagics.nim Normal file
View File

@@ -0,0 +1,66 @@
discard """
matrix: "--mm:refc"
output: '''
true
true
false
true
true
false
true
'''
joinable: false
"""
block tlowhigh:
type myEnum = enum e1, e2, e3, e4, e5
var a: array[myEnum, int]
for i in low(a) .. high(a):
a[i] = 0
proc sum(a: openArray[int]): int =
result = 0
for i in low(a)..high(a):
inc(result, a[i])
doAssert sum([1, 2, 3, 4]) == 10
block t8693:
type Foo = int | float
proc bar(t1, t2: typedesc): bool =
echo (t1 is t2)
(t2 is t1)
proc bar[T](x: T, t2: typedesc): bool =
echo (T is t2)
(t2 is T)
doAssert bar(int, Foo) == false
doAssert bar(4, Foo) == false
doAssert bar(any, int)
doAssert bar(int, any) == false
doAssert bar(Foo, Foo)
doAssert bar(any, Foo)
doAssert bar(Foo, any) == false
block t9442:
var v1: ref char
var v2: string
var v3: seq[char]
GC_ref(v1)
GC_unref(v1)
GC_ref(v2)
GC_unref(v2)
GC_ref(v3)
GC_unref(v3)
block: # bug #6499
let x = (chr, 0)
doAssert x[1] == 0
block: # bug #12229
proc foo(T: typedesc) = discard
foo(ref)

16
tests/system/tmemory.nim Normal file
View File

@@ -0,0 +1,16 @@
import std/assertions
block: # cmpMem
type
SomeHash = array[15, byte]
var
a: SomeHash
b: SomeHash
a[^1] = byte(1)
let c = a
doAssert cmpMem(a.addr, b.addr, sizeof(SomeHash)) > 0
doAssert cmpMem(b.addr, a.addr, sizeof(SomeHash)) < 0
doAssert cmpMem(a.addr, c.addr, sizeof(SomeHash)) == 0

61
tests/system/tnew.nim Normal file
View File

@@ -0,0 +1,61 @@
discard """
matrix: "--mm:refc; --mm:orc"
outputsub: '''
Simple tree node allocation worked!
Simple cycle allocation worked!
'''
joinable: false
"""
# Test the implementation of the new operator
# and the code generation for gc walkers
# (and the garbage collector):
type
PNode = ref TNode
TNode = object
data: int
str: string
le, ri: PNode
TStressTest = ref array[0..45, array[1..45, TNode]]
proc finalizer(n: PNode) =
write(stdout, n.data)
write(stdout, " is now freed\n")
proc newNode(data: int, le, ri: PNode): PNode =
when defined(gcDestructors): # using finalizer breaks the test for orc
new(result)
else:
new(result, finalizer)
result.le = le
result.ri = ri
result.data = data
# now loop and build a tree
proc main() =
var
i = 0
p: TStressTest
while i < 1000:
var n: PNode
n = newNode(i, nil, newNode(i + 10000, nil, nil))
inc(i)
new(p)
write(stdout, "Simple tree node allocation worked!\n")
i = 0
while i < 1000:
var m = newNode(i + 20000, nil, nil)
var k = newNode(i + 30000, nil, nil)
m.le = m
m.ri = k
k.le = m
k.ri = k
inc(i)
write(stdout, "Simple cycle allocation worked!\n")
main()

View File

@@ -0,0 +1,11 @@
discard """
output: 3
"""
var x: ref int
new(x)
x[] = 3
echo x[]

65
tests/system/tslices.nim Normal file
View File

@@ -0,0 +1,65 @@
discard """
output: '''
456456
456456
456456
Zugr5nd
egerichtetd
verichtetd
'''
"""
# Test the new slices.
var mystr = "Abgrund"
# mystr[..1] = "Zu" # deprecated
mystr[0..1] = "Zu"
mystr[4..4] = "5"
type
TEnum = enum e1, e2, e3, e4, e5, e6
var myarr: array[TEnum, int] = [1, 2, 3, 4, 5, 6]
myarr[e1..e3] = myarr[e4..e6]
# myarr[..e3] = myarr[e4..e6] # deprecated
myarr[0..e3] = myarr[e4..e6]
for x in items(myarr): stdout.write(x)
echo()
var myarr2: array[0..5, int] = [1, 2, 3, 4, 5, 6]
myarr2[0..2] = myarr2[3..5]
for x in items(myarr2): stdout.write(x)
echo()
var myseq = @[1, 2, 3, 4, 5, 6]
myseq[0..2] = myseq[^3 .. ^1]
for x in items(myseq): stdout.write(x)
echo()
echo mystr
mystr[4..4] = "u"
# test full replacement
# mystr[.. ^2] = "egerichtet" # deprecated
mystr[0 .. ^2] = "egerichtet"
echo mystr
mystr[0..2] = "ve"
echo mystr
var s = "abcdef"
s[1 .. ^2] = "xyz"
assert s == "axyzf"
# issue mentioned in PR #19219
type Foo = distinct uint64
# < here calls `pred` which used to cause a codegen error
# `pred` compiles because distinct ordinals are considered ordinal
const slice = 0 ..< 42.Foo