diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2018-06-27 00:08:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-27 00:08:21 +0200 |
commit | d8f7174ddaa19c0e88d1a3ffc851189be2f704df (patch) | |
tree | 732dfb1b8af0b3ce5f2731277fc43355918ad2ff /tests | |
parent | edc3806aa283c03aa6bae0d9300d381b1150d58d (diff) | |
parent | 101f23f3a65fabd5cba2f03070be1e157e572d79 (diff) | |
download | Nim-d8f7174ddaa19c0e88d1a3ffc851189be2f704df.tar.gz |
Merge pull request #7736 from cooldome/range_float_type
Language feature: range float types
Diffstat (limited to 'tests')
-rw-r--r-- | tests/float/tfloatrange.nim | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/float/tfloatrange.nim b/tests/float/tfloatrange.nim new file mode 100644 index 000000000..e8ea1912e --- /dev/null +++ b/tests/float/tfloatrange.nim @@ -0,0 +1,49 @@ +discard """ + cmd: "nim c -d:release --rangeChecks:on $file" + output: '''StrictPositiveRange +float +range fail expected +range fail expected +''' +""" +import math, fenv + +type + Positive = range[0.0..Inf] + StrictPositive = range[minimumPositiveValue(float)..Inf] + Negative32 = range[-maximumPositiveValue(float32) .. -1.0'f32] + +proc myoverload(x: float) = + echo "float" + +proc myoverload(x: Positive) = + echo "PositiveRange" + +proc myoverload(x: StrictPositive) = + echo "StrictPositiveRange" + +let x = 9.0.StrictPositive +myoverload(x) +myoverload(9.0) + +doAssert(sqrt(x) == 3.0) + +var z = -10.0 +try: + myoverload(StrictPositive(z)) +except: + echo "range fail expected" + + +proc strictOnlyProc(x: StrictPositive): bool = + if x > 1.0: true else: false + +let x2 = 5.0.Positive +doAssert(strictOnlyProc(x2)) + +try: + let x4 = 0.0.Positive + discard strictOnlyProc(x4) +except: + echo "range fail expected" + \ No newline at end of file |