-
Notifications
You must be signed in to change notification settings - Fork 1
/
testHomework2.6Randomly.hs
65 lines (57 loc) · 2.39 KB
/
testHomework2.6Randomly.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import System.Random
import Homework2
-- Used manually to get a random seed to pass into the test function. I couldn't
-- figure out how to use it in the test script.
--
newRand = randomIO :: IO Int
-- Expression to generate a pseudo-random sequece of doubles from [0 to 1).
randomList :: Int -> [Double]
randomList seed = randoms (mkStdGen seed) :: [Double]
-- A couple of random number seeds to play with. Can be generated by pasting the result of newRand.
-- Due to Haskell IO being not strictly-functional, this is the best I could figure out.
rndNums1 = randomList 3869386208656114178
rndNums2 = randomList 3078229163598965381
-- Accepts a random number sequence and ranges for radix and values. Generates a list of
-- test data [(r,n)].
--
getData :: Int -> [Double] -> Int -> Int -> Integer -> Integer -> [(Int,Integer)]
getData 0 _ _ _ _ _ = []
getData n (x1:x2:xs) rLow rHigh nLow nHigh = ((r1, n1) : getData (n -1) xs rLow rHigh nLow nHigh) where
r1 = rLow + ceiling (x1 * fromIntegral (rHigh - rLow))
n1 = nLow + ceiling (x2 * fromIntegral (nHigh - nLow))
-- Tests addition of Numerals.
-- Accepts the base 10 version of the numbers and corresponding base r Numeral values. Tests
-- by using Haskell Integer addition.
--
testAdd :: Integer -> Numeral -> Integer -> Numeral -> Bool
testAdd n1 v1 n2 v2 =
let
v3 = addLongInts v1 v2
t = n1 + n2
in
(evaluateLongInt v3) == t
-- Tests multiplication of Numerals.
-- Accepts the base 10 version of the numbers and corresponding base r Numeral values. Tests
-- by using Haskell Integer multiplication.
--
testMult :: Integer -> Numeral -> Integer -> Numeral -> Bool
testMult n1 v1 n2 v2 =
let
v3 = mulLongInts v1 v2
t = n1 * n2
in
(evaluateLongInt v3) == t
-- Expressions for generating random data sets.
--
data1 = getData 1000 rndNums1 2 256 0 (2^48)
data2 = getData 1000 rndNums2 2 256 0 2147483647
-- Zip them together to form pairs of terms for testing.
--
testData = zip data1 data2
testNumerals = foldl (\x ((r1, n1),(r2, n2)) -> (x &&
let
l1 = makeLongInt n1 r1
l2 = makeLongInt n2 r2
in
(testAdd n1 l1 n2 l2) && (testMult n1 l1 n2 l2)
)) True testData