Paste: Fails for bignum

Author: nomennescio
Mode: factor
Date: Tue, 14 Dec 2021 19:09:53
Plain Text |
USING: combinators kernel locals math sequences syntax ;
IN: s
: s2 ( n -- r ) dup 1 + * 2 / ; inline
MEMO:: s ( m n -- r ) n 1 = m zero? or [ 1 ] [ 
    m { { 0 [ 1 ] } { 1 [ n ] } { 2 [ n s2 ] } [ 1 - n <iota> reverse [ dupd 1 + s ] map-sum nip ] } case
  ] if
  ; inline recursive

! test code that passes (uses tools.testest framework, which is very similar to unit-test)

USING: s tools.testest math math.functions ;
IN: s.tests

: run-tests ( -- )
  "Sample Tests" describe#{
    "should work for small inputs" it#{
      <{ 1 1 s ->  1 }>
      <{ 0 53 s -> 1 }>
      <{ 1 49 s -> 49 }>
      <{ 1 101 s -> 101 }>
      <{ 2 5 s -> 15 }>
      <{ 2 99 s -> 4950 }>
      <{ 3 7 s -> 84 }>
      <{ 3 32 s -> 5984 }>
      <{ 4 8 s -> 330 }>
      <{ 5 17 s -> 20349 }>
      <{ 10 4 s -> 286 }>
      ! <{ 11 2 s -> 0 }>
      <{ 13 3 s -> 14 15 * 2 / }>
      <{ 13 2 s -> 14 }>
    }#
    
    "should work for edge cases" it#{
      <{ 0 1 s -> 1 }>
      <{ 1 1 s -> 1 }>
      <{ 0 10 100 ^ s -> 1 }>
      <{ 1 10 100 ^ s -> 10 100 ^ }>
    }#
  }#
;

MAIN: run-tests

Annotation: Full Tests

Author: Kacarott
Mode: factor
Date: Tue, 14 Dec 2021 19:16:07
Plain Text |
USING: s tools.testest locals math.functions kernel math math.ranges sequences random formatting ;
IN: s.tests

: randint ( m n -- r ) over - random + ;
: refsol-s-42248 ( m n -- res ) swap [0,b) dup rot 1 [ [ + ] curry map product ] bi-curry@ bi* /i ;
:: run-tests ( -- )
  "Sample Tests" describe#{
    "should work for small inputs" it#{
      <{ 1 1 s ->  1 }>
      <{ 0 53 s -> 1 }>
      <{ 1 49 s -> 49 }>
      <{ 1 101 s -> 101 }>
      <{ 2 5 s -> 15 }>
      <{ 2 99 s -> 4950 }>
      <{ 3 7 s -> 84 }>
      <{ 3 32 s -> 5984 }>
      <{ 4 8 s -> 330 }>
      <{ 5 17 s -> 20349 }>
      <{ 10 4 s -> 286 }>
    }#
    
    "should work for edge cases" it#{
      <{ 0 1 s -> 1 }>
      <{ 1 1 s -> 1 }>
      <{ 0 10 100 ^ s -> 1 }>
      <{ 1 10 100 ^ s -> 10 100 ^ }>
    }#
  }#
  
  "Random Tests" describe#{
    "Small Random Tests" describe#{
      25 [
        0 10 randint :> m
        1 100 randint :> n
      { m n } "Testing m=%d, n=%d" vsprintf it#{
        <{ m n s ->  m n refsol-s-42248 }>
      }#
      ] times
    }#
  
    "Big Random Tests" it#{
      125 [
        0 100 randint :> m
        1 10 100 ^ randint :> n
        <{ m n s ->  m n refsol-s-42248 }>
      ] times
    }#
  }#
;

MAIN: run-tests

Annotation: repro

Author: erg
Mode: factor
Date: Tue, 14 Dec 2021 20:59:12
Plain Text |
first-bignum <iota> reverse ! fails with same error, allocating a giant array

New Annotation

Summary:
Author:
Mode:
Body: