diff options
author | Araq <rumpf_a@web.de> | 2013-08-22 19:28:58 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-08-22 19:28:58 +0200 |
commit | a8c8a85135e73777ea2c115bf1352456c1dd69aa (patch) | |
tree | a06a9a97f6e98480d98d6b063dfe224951c233db /tests/compile | |
parent | cf38d635bf8e94abbc68cca55fd6deb6ebc3da5d (diff) | |
parent | ca3a4ce6721c87cfcbb9eb02002ecf8aeb89233c (diff) | |
download | Nim-a8c8a85135e73777ea2c115bf1352456c1dd69aa.tar.gz |
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'tests/compile')
-rw-r--r-- | tests/compile/tforwardgeneric.nim | 5 | ||||
-rw-r--r-- | tests/compile/tgeneric4.nim | 10 | ||||
-rw-r--r-- | tests/compile/tgenericdefaults.nim | 29 | ||||
-rw-r--r-- | tests/compile/tobjects.nim | 86 | ||||
-rw-r--r-- | tests/compile/toverprc.nim | 54 | ||||
-rw-r--r-- | tests/compile/ttypeclasses.nim | 21 |
6 files changed, 133 insertions, 72 deletions
diff --git a/tests/compile/tforwardgeneric.nim b/tests/compile/tforwardgeneric.nim index ef263d733..c5943b966 100644 --- a/tests/compile/tforwardgeneric.nim +++ b/tests/compile/tforwardgeneric.nim @@ -1,5 +1,5 @@ discard """ - output: "1.0000000000000000e+00 10" + output: "1.1000000000000001e+00 11" ccodecheck: "!@'ClEnv'" """ @@ -8,5 +8,6 @@ proc p[T](a, b: T): T echo p(0.9, 0.1), " ", p(9, 1) proc p[T](a, b: T): T = - result = a + b + let c = b + result = a + b + c diff --git a/tests/compile/tgeneric4.nim b/tests/compile/tgeneric4.nim new file mode 100644 index 000000000..f79096636 --- /dev/null +++ b/tests/compile/tgeneric4.nim @@ -0,0 +1,10 @@ +type + TIDGen*[A: Ordinal] = object + next: A + free: seq[A] + +proc newIDGen*[A]: TIDGen[A] = + newSeq result.free, 0 + +var x = newIDGen[int]() + diff --git a/tests/compile/tgenericdefaults.nim b/tests/compile/tgenericdefaults.nim new file mode 100644 index 000000000..ad96f1851 --- /dev/null +++ b/tests/compile/tgenericdefaults.nim @@ -0,0 +1,29 @@ +type + TFoo[T, U, R = int] = object + x: T + y: U + z: R + + TBar[T] = TFoo[T, array[4, T], T] + +var x1: TFoo[int, float] + +static: + assert type(x1.x) is int + assert type(x1.y) is float + assert type(x1.z) is int + +var x2: TFoo[string, R = float, U = seq[int]] + +static: + assert type(x2.x) is string + assert type(x2.y) is seq[int] + assert type(x2.z) is float + +var x3: TBar[float] + +static: + assert type(x3.x) is float + assert type(x3.y) is array[4, float] + assert type(x3.z) is float + diff --git a/tests/compile/tobjects.nim b/tests/compile/tobjects.nim index 68705d944..06fa15583 100644 --- a/tests/compile/tobjects.nim +++ b/tests/compile/tobjects.nim @@ -1,52 +1,52 @@ -type - TBase = object of TObject - x, y: int - - TSubclassKind = enum ka, kb, kc, kd, ke, kf - TSubclass = object of TBase - case c: TSubclassKind - of ka, kb, kc, kd: - a, b: int - of ke: - d, e, f: char - else: nil - n: bool +type + TBase = object of TObject + x, y: int + + TSubclassKind = enum ka, kb, kc, kd, ke, kf + TSubclass = object of TBase + case c: TSubclassKind + of ka, kb, kc, kd: + a, b: int + of ke: + d, e, f: char + else: nil + n: bool type TMyObject = object of TObject - case disp: range[0..4]: + case disp: range[0..4] of 0: arg: char of 1: s: string else: wtf: bool var x: TMyObject - -var - global: int - -var - s: string - r: float = 0.0 - i: int = 500 + 400 - -case i -of 500..999: write(stdout, "ha!\n") -of 1000..3000, 12: write(stdout, "ganz schön groß\n") -of 1, 2, 3: write(stdout, "1 2 oder 3\n") -else: write(stdout, "sollte nicht passieren\n") - -case readLine(stdin) -of "Rumpf": write(stdout, "Hallo Meister!\n") -of "Andreas": write(stdout, "Hallo Meister!\n") -else: write(stdout, "Nicht mein Meister!\n") - -global = global + 1 -write(stdout, "Hallo wie heißt du? \n") -s = readLine(stdin) -i = 0 -while i < len(s): - if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n") - i = i + 1 - -write(stdout, "Du heißt " & s) + +var + global: int + +var + s: string + r: float = 0.0 + i: int = 500 + 400 + +case i +of 500..999: write(stdout, "ha!\n") +of 1000..3000, 12: write(stdout, "ganz schön groß\n") +of 1, 2, 3: write(stdout, "1 2 oder 3\n") +else: write(stdout, "sollte nicht passieren\n") + +case readLine(stdin) +of "Rumpf": write(stdout, "Hallo Meister!\n") +of "Andreas": write(stdout, "Hallo Meister!\n") +else: write(stdout, "Nicht mein Meister!\n") + +global = global + 1 +write(stdout, "Hallo wie heißt du? \n") +s = readLine(stdin) +i = 0 +while i < len(s): + if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n") + i = i + 1 + +write(stdout, "Du heißt " & s) diff --git a/tests/compile/toverprc.nim b/tests/compile/toverprc.nim index 96b851fc1..22b64ed48 100644 --- a/tests/compile/toverprc.nim +++ b/tests/compile/toverprc.nim @@ -1,32 +1,32 @@ -# Test overloading of procs when used as function pointers - -import strutils - -proc parseInt(x: float): int {.noSideEffect.} = nil -proc parseInt(x: bool): int {.noSideEffect.} = nil -proc parseInt(x: float32): int {.noSideEffect.} = nil -proc parseInt(x: int8): int {.noSideEffect.} = nil -proc parseInt(x: TFile): int {.noSideEffect.} = nil -proc parseInt(x: char): int {.noSideEffect.} = nil -proc parseInt(x: int16): int {.noSideEffect.} = nil +# Test overloading of procs when used as function pointers + +import strutils + +proc parseInt(x: float): int {.noSideEffect.} = nil +proc parseInt(x: bool): int {.noSideEffect.} = nil +proc parseInt(x: float32): int {.noSideEffect.} = nil +proc parseInt(x: int8): int {.noSideEffect.} = nil +proc parseInt(x: TFile): int {.noSideEffect.} = nil +proc parseInt(x: char): int {.noSideEffect.} = nil +proc parseInt(x: int16): int {.noSideEffect.} = nil proc parseInt[T](x: T): int = echo x; 34 - -type - TParseInt = proc (x: string): int {.noSideEffect.} - -var - q = TParseInt(parseInt) - p: TParseInt = parseInt - -proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int = - result = x("123") - -echo "Give a list of numbers (separated by spaces): " -var x = stdin.readline.split.map(parseInt).max -echo x, " is the maximum!" -echo "another number: ", takeParseInt(parseInt) - + +type + TParseInt = proc (x: string): int {.noSideEffect.} + +var + q = TParseInt(parseInt) + p: TParseInt = parseInt + +proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int = + result = x("123") + +echo "Give a list of numbers (separated by spaces): " +var x = stdin.readline.split.map(parseInt).max +echo x, " is the maximum!" +echo "another number: ", takeParseInt(parseInt) + type TFoo[a,b] = object diff --git a/tests/compile/ttypeclasses.nim b/tests/compile/ttypeclasses.nim index 5e23e8489..677229868 100644 --- a/tests/compile/ttypeclasses.nim +++ b/tests/compile/ttypeclasses.nim @@ -5,6 +5,8 @@ type T1 = expr T2 = expr + Numeric = int|float + proc takesExpr(x, y) = echo x, y @@ -32,3 +34,22 @@ takesFoo(f, f) takes2Types(1, 1, "string") takes2Types[string, int]("test", "test", 1) +proc takesSeq(x: seq) = + echo "seq" + +takesSeq(@[1, 2, 3]) +takesSeq(@["x", "y", "z"]) + +proc takesSeqOfFoos(x: seq[TFoo]) = + echo "foo seq" + +var sf = newSeq[TFoo[int]](3) + +takesSeq(sf) +takesSeqOfFoos(sf) + +proc takesFooOfNumeric(x: TFoo[Numeric]) = + echo "foo of numeric" + +takesFooOfNumeric(sf[0]) + |