make at least bootstrapping work

This commit is contained in:
Araq 2018-08-05 14:17:24 +02:00
commit 7ac6462cbd
81 changed files with 1314 additions and 425 deletions

View file

@ -1,15 +0,0 @@
discard """
file: "t6100.nim"
exitcode: 0
output: "10000000"
"""
import asyncdispatch
let done = newFuture[int]()
done.complete(1)
proc asyncSum: Future[int] {.async.} =
for _ in 1..10_000_000:
result += await done
echo waitFor asyncSum()

View file

@ -1,19 +0,0 @@
discard """
file: "t7985.nim"
exitcode: 0
output: "(value: 1)"
"""
import json, asyncdispatch
proc getData(): Future[JsonNode] {.async.} =
result = %*{"value": 1}
type
MyData = object
value: BiggestInt
proc main() {.async.} =
let data = to(await(getData()), MyData)
echo data
waitFor(main())

View file

@ -0,0 +1,47 @@
discard """
exitcode: 0
output: "ok"
"""
import json, asyncdispatch
block: #6100
let done = newFuture[int]()
done.complete(1)
proc asyncSum: Future[int] {.async.} =
for _ in 1..10_000_000:
result += await done
let res = waitFor asyncSum()
doAssert(res == 10000000)
block: #7985
proc getData(): Future[JsonNode] {.async.} =
result = %*{"value": 1}
type
MyData = object
value: BiggestInt
proc main() {.async.} =
let data = to(await(getData()), MyData)
doAssert($data == "(value: 1)")
waitFor(main())
block: #8399
proc bar(): Future[string] {.async.} = discard
proc foo(line: string) {.async.} =
var res =
case line[0]
of '+', '-': @[]
of '$': (let x = await bar(); @[""])
else:
nil
doAssert(res == @[""])
waitFor foo("$asd")
echo "ok"

View file

@ -15,7 +15,7 @@ proc main =
let val = s[i]()
if val != $(i*i): echo "bug ", val
if getOccupiedMem() > 3000_000: quit("still a leak!")
if getOccupiedMem() > 5000_000: quit("still a leak!")
echo "success"
main()

16
tests/errmsgs/t8434.nim Normal file
View file

@ -0,0 +1,16 @@
discard """
errormsg: "type mismatch: got <byte, int literal(0)>"
nimout: '''but expected one of:
proc fun0[T1: int | float |
object | array | seq](a1: T1; a2: int)
first type mismatch at position: 1
required type: T1: int or float or object or array or seq
but expression 'byte(1)' is of type: byte
expression: fun0(byte(1), 0)
'''
"""
proc fun0[T1:int|float|object|array|seq](a1:T1, a2:int)=discard
fun0(byte(1), 0)

View file

@ -16,9 +16,12 @@ proc main =
dealloc p
# c_fprintf(stdout, "iteration: %ld size: %ld\n", i, size)
when defined(cpu64):
# bug #7120
var x = alloc(((1 shl 29) - 4) * 8)
dealloc x
# see https://github.com/nim-lang/Nim/issues/8509
# this often made appveyor (on windows) fail with out of memory
when defined(posix):
# bug #7120
var x = alloc(((1 shl 29) - 4) * 8)
dealloc x
main()

View file

@ -14,7 +14,7 @@ proc handleRequest(query: string): StringTableRef =
let x = foo
result = x()
const Limit = when compileOption("gc", "markAndSweep"): 5*1024*1024 else: 700_000
const Limit = 5*1024*1024
proc main =
var counter = 0

11
tests/generics/t8403.nim Normal file
View file

@ -0,0 +1,11 @@
discard """
output: "6.0"
"""
proc sum*[T](s: seq[T], R: typedesc): R =
var sum: R = 0
for x in s:
sum += R(x)
return sum
echo @[1, 2, 3].sum(float)

10
tests/generics/t8439.nim Normal file
View file

@ -0,0 +1,10 @@
discard """
output: "1"
"""
type
Cardinal = enum
north, east, south, west
proc foo[cardinal: static[Cardinal]](): int = 1
echo(foo[north]())

20
tests/iter/t338.nim Normal file
View file

@ -0,0 +1,20 @@
discard """
output: '''0
1
2
3
4
'''
"""
proc moo(): iterator (): int =
iterator fooGen: int {.closure.} =
while true:
yield result
result.inc
return fooGen
var foo = moo()
for i in 0 .. 4:
echo foo()

View file

@ -1,4 +1,9 @@
discard """
msg: '''initApple
deinitApple
Coral
enum
redCoral, blackCoral'''
output: '''TFoo
TBar'''
"""
@ -23,3 +28,40 @@ macro test: untyped =
bindSym("TBar"))
test()
# issue 7827, bindSym power up
{.experimental: "dynamicBindSym".}
type
Apple = ref object
name: string
color: int
weight: int
proc initApple(name: string): Apple =
discard
proc deinitApple(x: Apple) =
discard
macro wrapObject(obj: typed, n: varargs[untyped]): untyped =
let m = n[0]
for x in m:
var z = bindSym x
echo z.repr
wrapObject(Apple):
initApple
deinitApple
type
Coral = enum
redCoral
blackCoral
macro mixer(): untyped =
let m = "Co" & "ral"
let x = bindSym(m)
echo x.repr
echo getType(x).repr
mixer()

View file

@ -43,4 +43,35 @@ macro repr_and_parse(fn: typed): typed =
echo fn_impl.repr
result = parseStmt(fn_impl.repr)
repr_and_parse(f)
repr_and_parse(f)
#------------------------------------
# bugs #8343 and #8344
proc one_if_proc(x, y : int): int =
if x < y: result = x
else: result = y
proc test_block(x, y : int): int =
block label:
result = x
result = y
#------------------------------------
# bugs #8348
template `>`(x, y: untyped): untyped =
## "is greater" operator. This is the same as ``y < x``.
y < x
proc test_cond_stmtlist(x, y: int): int =
result = x
if x > y:
result = x
repr_and_parse(one_if_proc)
repr_and_parse(test_block)
repr_and_parse(test_cond_stmtlist)

View file

@ -1,5 +1,7 @@
# Test the sizeof proc
discard """
file: "tsize.nim"
output: "40 3 12 32"
"""
type
TMyRecord {.final.} = object
x, y: int
@ -7,4 +9,20 @@ type
r: float
s: string
TMyEnum = enum
tmOne, tmTwo, tmThree, tmFour
TMyArray1 = array[3, uint8]
TMyArray2 = array[1..3, int32]
TMyArray3 = array[TMyEnum, float64]
const
mysize1 = sizeof(TMyArray1)
mysize2 = sizeof(TMyArray2)
mysize3 = sizeof(TMyArray3)
write(stdout, sizeof(TMyRecord))
echo ' ', mysize1, ' ', mysize2, ' ',mysize3

View file

@ -154,3 +154,23 @@ block:
let a: proc(x: int) {.defaultValue(5).} = nil
static:
doAssert hasCustomPragma(a.type, defaultValue)
# bug #8371
template thingy {.pragma.}
type
Cardinal = enum
north, east, south, west
Something = object
a: float32
case cardinal: Cardinal
of north:
b {.thingy.}: int
of east:
c: int
of south: discard
else: discard
var foo: Something
foo.cardinal = north
doAssert foo.b.hasCustomPragma(thingy) == true

22
tests/sets/tsetpop.nim Normal file
View file

@ -0,0 +1,22 @@
discard """
targets: "c c++ js"
output: '''1000
0
set is empty
'''
"""
import sets
var a = initSet[int]()
for i in 1..1000:
a.incl(i)
echo len(a)
for i in 1..1000:
discard a.pop()
echo len(a)
try:
echo a.pop()
except KeyError as e:
echo e.msg

View file

@ -205,4 +205,12 @@ echo warnUninit in gNotes
# 7555
doAssert {-1.int8, -2, -2}.card == 2
doAssert {1, 2, 2, 3..5, 4..6}.card == 6
doAssert {1, 2, 2, 3..5, 4..6}.card == 6
type Foo = enum
Foo1 = 0
Foo2 = 1
Foo3 = 3
let x = { Foo1, Foo2 }
# bug #8425

View file

@ -0,0 +1,21 @@
discard """
output: '''OK'''
"""
#bug #8468
import encodings, strutils
when defined(windows):
var utf16to8 = open(destEncoding = "utf-16", srcEncoding = "utf-8")
var s = "some string"
var c = utf16to8.convert(s)
var z = newStringOfCap(s.len * 2)
for x in s:
z.add x
z.add chr(0)
doAssert z == c
echo "OK"

View file

@ -337,7 +337,7 @@ when isMainModule:
n2: Option[int]
n3: Option[string]
n4: Option[bool]
var j0 = parseJson("""{"n1": 1, "n2": null, "n3": null, "n4": null}""")
let j0Deser = j0.to(Obj)
doAssert j0Deser.n1 == 1
@ -411,10 +411,109 @@ when isMainModule:
doAssert dataDeser.a == 1
doAssert dataDeser.f == 6
doAssert dataDeser.i == 9.9'f32
# deserialize directly into a table
block:
let s = """{"a": 1, "b": 2}"""
let t = parseJson(s).to(Table[string, int])
doAssert t["a"] == 1
doAssert t["b"] == 2
doAssert t["b"] == 2
block:
# bug #8037
type
Apple = distinct string
String = distinct Apple
Email = distinct string
MyList = distinct seq[int]
MyYear = distinct Option[int]
MyTable = distinct Table[string, int]
MyArr = distinct array[3, float]
MyRef = ref object
name: string
MyObj = object
color: int
MyDistRef = distinct MyRef
MyDistObj = distinct MyObj
Toot = object
name*: String
email*: Email
list: MyList
year: MyYear
dict: MyTable
arr: MyArr
person: MyDistRef
distfruit: MyDistObj
dog: MyRef
fruit: MyObj
emails: seq[String]
var tJson = parseJson("""
{
"name":"Bongo",
"email":"bongo@bingo.com",
"list": [11,7,15],
"year": 1975,
"dict": {"a": 1, "b": 2},
"arr": [1.0, 2.0, 7.0],
"person": {"name": "boney"},
"dog": {"name": "honey"},
"fruit": {"color": 10},
"distfruit": {"color": 11},
"emails": ["abc", "123"]
}
""")
var t = to(tJson, Toot)
doAssert string(t.name) == "Bongo"
doAssert string(t.email) == "bongo@bingo.com"
doAssert seq[int](t.list) == @[11,7,15]
doAssert Option[int](t.year).get() == 1975
doAssert Table[string,int](t.dict)["a"] == 1
doAssert Table[string,int](t.dict)["b"] == 2
doAssert array[3, float](t.arr) == [1.0,2.0,7.0]
doAssert MyRef(t.person).name == "boney"
doAssert MyObj(t.distFruit).color == 11
doAssert t.dog.name == "honey"
doAssert t.fruit.color == 10
doAssert seq[string](t.emails) == @["abc", "123"]
block test_table:
var y = parseJson("""{"a": 1, "b": 2, "c": 3}""")
var u = y.to(MyTable)
var v = y.to(Table[string, int])
doAssert Table[string, int](u)["a"] == 1
doAssert Table[string, int](u)["b"] == 2
doAssert Table[string, int](u)["c"] == 3
doAssert v["a"] == 1
block primitive_string:
const kApple = "apple"
var u = newJString(kApple)
var v = u.to(Email)
var w = u.to(Apple)
var x = u.to(String)
doAssert string(v) == kApple
doAssert string(w) == kApple
doAssert string(x) == kApple
block test_option:
var u = newJInt(1137)
var v = u.to(MyYear)
var w = u.to(Option[int])
doAssert Option[int](v).get() == 1137
doAssert w.get() == 1137
block test_object:
var u = parseJson("""{"color": 987}""")
var v = u.to(MyObj)
var w = u.to(MyDistObj)
doAssert v.color == 987
doAssert MyObj(w).color == 987
block test_ref_object:
var u = parseJson("""{"name": "smith"}""")
var v = u.to(MyRef)
var w = u.to(MyDistRef)
doAssert v.name == "smith"
doAssert MyRef(w).name == "smith"

View file

@ -145,7 +145,7 @@ else:
echo getLastModificationTime("a") == tm
removeFile("a")
when defined(Linux) or defined(macosx):
when defined(posix):
block normalizedPath:
block relative:

18
tests/system/t7894.nim Normal file
View file

@ -0,0 +1,18 @@
discard """
"""
import os
const size = 250000000
var saved = newSeq[seq[int8]]()
for i in 0..22:
# one of these is 0.25GB.
#echo i
var x = newSeq[int8](size)
sleep(10)
saved.add(x)
for x in saved:
#echo x.len
doAssert x.len == size

View file

@ -1,54 +1,57 @@
discard """
"""
var x: ptr int
x = cast[ptr int](alloc(7))
assert x != nil
doAssert x != nil
x = cast[ptr int](x.realloc(2))
assert x != nil
doAssert x != nil
x.dealloc()
x = createU(int, 3)
assert x != nil
doAssert x != nil
x.dealloc()
x = create(int, 4)
assert cast[ptr array[4, int]](x)[0] == 0
assert cast[ptr array[4, int]](x)[1] == 0
assert cast[ptr array[4, int]](x)[2] == 0
assert cast[ptr array[4, int]](x)[3] == 0
doAssert cast[ptr array[4, int]](x)[0] == 0
doAssert cast[ptr array[4, int]](x)[1] == 0
doAssert cast[ptr array[4, int]](x)[2] == 0
doAssert cast[ptr array[4, int]](x)[3] == 0
x = x.resize(4)
assert x != nil
doAssert x != nil
x.dealloc()
x = cast[ptr int](allocShared(100))
assert x != nil
doAssert x != nil
deallocShared(x)
x = createSharedU(int, 3)
assert x != nil
doAssert x != nil
x.deallocShared()
x = createShared(int, 3)
assert x != nil
assert cast[ptr array[3, int]](x)[0] == 0
assert cast[ptr array[3, int]](x)[1] == 0
assert cast[ptr array[3, int]](x)[2] == 0
doAssert x != nil
doAssert cast[ptr array[3, int]](x)[0] == 0
doAssert cast[ptr array[3, int]](x)[1] == 0
doAssert cast[ptr array[3, int]](x)[2] == 0
assert x != nil
doAssert x != nil
x = cast[ptr int](x.resizeShared(2))
assert x != nil
doAssert x != nil
x.deallocShared()
x = create(int, 10)
assert x != nil
doAssert x != nil
x = x.resize(12)
assert x != nil
doAssert x != nil
x.dealloc()
x = createShared(int, 1)
assert x != nil
doAssert x != nil
x = x.resizeShared(1)
assert x != nil
doAssert x != nil
x.deallocShared()
x = cast[ptr int](alloc0(125 shl 23))

View file

@ -1,3 +1,6 @@
discard """
"""
const
nmax = 2*1024*1024*1024

View file

@ -65,7 +65,7 @@ proc main() =
for val in table.values():
if myObj2.isNil:
myObj2 = val
assert(myObj == myObj2) # passes
doAssert(myObj == myObj2) # passes
var tableCopy: ListTableRef[int, SomeObj]
deepCopy(tableCopy, table)
@ -80,7 +80,7 @@ proc main() =
#echo cast[int](myObjCopy)
#echo cast[int](myObjCopy2)
assert(myObjCopy == myObjCopy2) # fails
doAssert(myObjCopy == myObjCopy2) # fails
type
@ -88,7 +88,7 @@ type
counter, max: int
data: array[0..99, (pointer, pointer)]
assert(sizeof(PtrTable) == 2*sizeof(int)+sizeof(pointer)*2*100)
doAssert(sizeof(PtrTable) == 2*sizeof(int)+sizeof(pointer)*2*100)
main()
echo "ok"

View file

@ -1,3 +1,6 @@
discard """
"""
import
unittest, osproc, streams, os, strformat
const STRING_DATA = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
@ -36,7 +39,7 @@ proc verifyFileSize(sz: int64) =
discard execProcess(&"dd if=/dev/zero of={fn} bs=1000000 count={size_in_mb}")
doAssert os.getFileSize(fn) == sz # Verify OS filesize by string
var f = open(fn)
doAssert f.getFileSize() == sz # Verify file handle filesize
f.close()

View file

@ -1,3 +1,6 @@
discard """
"""
import os
import osproc
import parseopt2
@ -7,12 +10,12 @@ let argv = commandLineParams()
if argv == @[]:
# this won't work with spaces
assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar=a --a=c:d --ab -c --a[baz]:doo") == 0
doAssert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar=a --a=c:d --ab -c --a[baz]:doo") == 0
else:
let f = toSeq(getopt())
echo f.repr
assert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == ""
assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar=a"
assert f[2].kind == cmdLongOption and f[2].key == "a=c" and f[2].val == "d"
assert f[3].kind == cmdLongOption and f[3].key == "ab" and f[3].val == ""
assert f[4].kind == cmdShortOption and f[4].key == "c" and f[4].val == ""
doAssert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == ""
doAssert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar=a"
doAssert f[2].kind == cmdLongOption and f[2].key == "a=c" and f[2].val == "d"
doAssert f[3].kind == cmdLongOption and f[3].key == "ab" and f[3].val == ""
doAssert f[4].kind == cmdShortOption and f[4].key == "c" and f[4].val == ""

View file

@ -54,4 +54,9 @@ static:
new(s)
var ss = s
s[] = 1
doAssert ss[] == 1
doAssert ss[] == 1
static: # bug #8402
type R = ref object
var empty: R
let otherEmpty = empty