From cb5623ee36a2264796836f2a1b6dc0968725f963 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 5 Sep 2024 17:32:50 +0300 Subject: [PATCH] feat(#3251): added random tests --- .../java/org/eolang/parser/ParsingTrain.java | 1 - .../src/main/eo/org/eolang/math/random.eo | 29 +++++---- .../EOsys/Posix/GettimeofdaySyscall.java | 2 +- .../EOsys/Win32/GetSystemTimeFuncCall.java | 2 +- .../test/eo/org/eolang/math/random-tests.eo | 62 +++++++++++++++++++ 5 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 eo-runtime/src/test/eo/org/eolang/math/random-tests.eo diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java b/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java index 38a029935a..d1002324bb 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java +++ b/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java @@ -68,7 +68,6 @@ public final class ParsingTrain extends TrEnvelope { private static final String[] SHEETS = { "/org/eolang/parser/errors/not-empty-atoms.xsl", "/org/eolang/parser/critical-errors/duplicate-names.xsl", - "/org/eolang/parser/errors/many-free-attributes.xsl", "/org/eolang/parser/errors/broken-aliases.xsl", "/org/eolang/parser/errors/duplicate-aliases.xsl", "/org/eolang/parser/errors/global-nonames.xsl", diff --git a/eo-runtime/src/main/eo/org/eolang/math/random.eo b/eo-runtime/src/main/eo/org/eolang/math/random.eo index faa2647f93..1ba0374aa4 100644 --- a/eo-runtime/src/main/eo/org/eolang/math/random.eo +++ b/eo-runtime/src/main/eo/org/eolang/math/random.eo @@ -30,14 +30,14 @@ +rt node eo2js-runtime:0.0.0 +version 0.0.0 -# Random number. +# Pseudo random number. [seed] > random + $ > fixed # Get float value for current `seed`. # Divide `seed` to maximum possible value of the `seed` which is 1 << 53 div. > @ seed.as-number 00-20-00-00-00-00-00-00.as-i64.as-number - # Next random. # Formula is based on linear congruential pseudorandom number generator, as defined by # D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2, @@ -45,17 +45,16 @@ # Magic numbers are taken from Java implementation. 48 lower bits are considered. # `next` = (`seed` * 25214903917 + 11) & ((1 << 48) - 1). # Here `00-0F-FF-FF-FF-FF-FF-FF` is pre calculated `(1 << 48) - 1`. - [] > next - as-number. > @ - random > - as-number. - as-i64. - and. - as-i64. - plus. - ^.seed.times 25214903917 - 11 - 00-0F-FF-FF-FF-FF-FF-FF + fixed. > next + random + as-number. + as-i64. + and. + as-i64. + plus. + seed.times 25214903917 + 11 + 00-0F-FF-FF-FF-FF-FF-FF # New random with pseudo-random seed. [] > pseudo @@ -83,9 +82,9 @@ as-i64. if. os.is-windows - (win32 "GetSystemTime").milliseconds + (win32 "GetSystemTime" *).milliseconds [] - (posix "gettimeofday").output > timeval + (posix "gettimeofday" *).output > timeval plus. > @ timeval.tv-sec.times 1000 as-number. diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/GettimeofdaySyscall.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/GettimeofdaySyscall.java index 64af3ab7ce..7439ce31e8 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/GettimeofdaySyscall.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/GettimeofdaySyscall.java @@ -69,7 +69,7 @@ public Phi make(final Phi... params) { * Timeval structure. * @since 0.40.0 */ - static class Timeval extends Structure { + public static class Timeval extends Structure { /** * Seconds since Jan. 1, 1970 */ diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/GetSystemTimeFuncCall.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/GetSystemTimeFuncCall.java index 78c72d2cc5..67a5fa4977 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/GetSystemTimeFuncCall.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/GetSystemTimeFuncCall.java @@ -77,7 +77,7 @@ public Phi make(final Phi... params) { * System time structure. * @since 0.40.0 */ - static class SystemTime extends Structure { + public static class SystemTime extends Structure { /** * Year. */ diff --git a/eo-runtime/src/test/eo/org/eolang/math/random-tests.eo b/eo-runtime/src/test/eo/org/eolang/math/random-tests.eo new file mode 100644 index 0000000000..0c3b3b9cd6 --- /dev/null +++ b/eo-runtime/src/test/eo/org/eolang/math/random-tests.eo @@ -0,0 +1,62 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.math.random ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++tests ++package org.eolang.math ++version 0.0.0 + +# Test. +[] > random-with-seed + random 51 > r + not. > @ + eq. + r.next + r.next.next + +# Test. +[] > seeded-randoms-are-equal + eq. > @ + (random 1654).next.next.next + (random 1654).next.next.next + +# Test. +[] > random-is-in-range + (random 123).fixed > r + and. > @ + and. + and. + r.lt 1 + (r.lt 0).not + and. + r.next.lt 1 + (r.next.lt 0).not + and. + r.next.next.lt 1 + (r.next.next.lt 0).not + +# Test. +[] > two-random-numbers-not-equal + not. > @ + random.pseudo.eq random.pseudo