File: Network.hs

package info (click to toggle)
haskell-foundation 0.0.30-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 932 kB
  • sloc: haskell: 9,124; ansic: 570; makefile: 7
file content (74 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (4)
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
66
67
68
69
70
71
72
73
74
-- |
-- Module:
-- Author: Nicolas Di Prima <nicolas>
-- Date:   2017-01-18T17:34:06+00:00
-- Email:  nicolasdiprima@gmail.com
--

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}

module Test.Data.Network
    ( genIPv4
    , genIPv4Tuple
    , genIPv4String
    , genIPv6
    , genIPv6Tuple
    , genIPv6String
    ) where

import Foundation
import Foundation.Check
import Foundation.Network.IPv4 as IPv4
import Foundation.Network.IPv6 as IPv6
import Foundation.Class.Storable as F
import qualified Foreign.Storable as Foreign

instance Arbitrary IPv4 where
    arbitrary = genIPv4
instance Foreign.Storable IPv4 where
    sizeOf a = let CountOf b = F.size (Just a) in b
    alignment a = let CountOf b = F.alignment (Just a) in b
    peek = F.peek
    poke = F.poke
instance Arbitrary IPv6 where
    arbitrary = genIPv6
instance Foreign.Storable IPv6 where
    sizeOf a = let CountOf b = F.size (Just a) in b
    alignment a = let CountOf b = F.alignment (Just a) in b
    peek = F.peek
    poke = F.poke

genIPv4Tuple :: Gen (Word8, Word8, Word8, Word8)
genIPv4Tuple =
    (,,,) <$> arbitrary
          <*> arbitrary
          <*> arbitrary
          <*> arbitrary

genIPv6Tuple :: Gen (Word16, Word16, Word16, Word16, Word16, Word16, Word16, Word16)
genIPv6Tuple =
    (,,,,,,,) <$> arbitrary
              <*> arbitrary
              <*> arbitrary
              <*> arbitrary
              <*> arbitrary
              <*> arbitrary
              <*> arbitrary
              <*> arbitrary

genIPv4String :: Gen String
genIPv4String = do
    (w1, w2, w3, w4) <- genIPv4Tuple
    return $ show w1 <> "." <> show w2 <> "." <> show w3 <> "." <> show w4

genIPv6String :: Gen String
genIPv6String = IPv6.toString <$> genIPv6

genIPv6 :: Gen IPv6
genIPv6 = IPv6.fromTuple <$> genIPv6Tuple

-- | a better generator for unicode Character
genIPv4 :: Gen IPv4
genIPv4 = IPv4.fromTuple <$> genIPv4Tuple