Skip to content

Commit

Permalink
feat(#3251): added random tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Sep 5, 2024
1 parent 48ea421 commit cb5623e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 14 additions & 15 deletions eo-runtime/src/main/eo/org/eolang/math/random.eo
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,31 @@
+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,
# Third edition: Seminumerical Algorithms, section 3.2.1.
# 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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
62 changes: 62 additions & 0 deletions eo-runtime/src/test/eo/org/eolang/math/random-tests.eo
Original file line number Diff line number Diff line change
@@ -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 [email protected]
+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

0 comments on commit cb5623e

Please sign in to comment.