|
| 1 | +;; -*- coding: utf-8 -*- |
| 2 | +;; |
| 3 | +;; Copyright (c) 2010-2015 Tuukka Turto |
| 4 | +;; |
| 5 | +;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +;; of this software and associated documentation files (the "Software"), to deal |
| 7 | +;; in the Software without restriction, including without limitation the rights |
| 8 | +;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +;; copies of the Software, and to permit persons to whom the Software is |
| 10 | +;; furnished to do so, subject to the following conditions: |
| 11 | +;; |
| 12 | +;; The above copyright notice and this permission notice shall be included in |
| 13 | +;; all copies or substantial portions of the Software. |
| 14 | +;; |
| 15 | +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +;; THE SOFTWARE. |
| 22 | + |
| 23 | +(require archimedes) |
| 24 | +(require pyherc.macros) |
| 25 | + |
| 26 | +(import [hamcrest [assert-that is-not :as is-not- is- equal-to |
| 27 | + has-length has-items]] |
| 28 | + [hypothesis.strategies [integers]] |
| 29 | + [pyherc.markov [chain-factory]]) |
| 30 | + |
| 31 | +(background infinite-foo-chain |
| 32 | + [factory (chain-factory :start-elements [#t("foo" 1 100)] |
| 33 | + :elements {"foo" [#t("foo" 1 100)]})] |
| 34 | + [chain (factory)]) |
| 35 | + |
| 36 | +(fact "first element of chain can be read" |
| 37 | + (with-background infinite-foo-chain [chain] |
| 38 | + (assert-that (first chain) (is- (equal-to "foo"))))) |
| 39 | + |
| 40 | +(fact "taking n elements from infinite chain will not exhaust it" |
| 41 | + (variants :n (integers :min-value 2)) |
| 42 | + (with-background infinite-foo-chain [chain] |
| 43 | + (drop n chain) |
| 44 | + (assert-that (first chain) (is- (equal-to "foo"))))) |
| 45 | + |
| 46 | +(background one-element-chain |
| 47 | + [factory (chain-factory :start-elements [#t("foo" 1 100)] |
| 48 | + :elements {"foo" []})] |
| 49 | + [chain (factory)]) |
| 50 | + |
| 51 | +(fact "one element markov chain contains only one element" |
| 52 | + (variants :n (integers :min-value 1)) |
| 53 | + (example :n 1) |
| 54 | + (with-background one-element-chain [chain] |
| 55 | + (assert-that (list (take 10 chain)) |
| 56 | + (has-length 1)))) |
| 57 | + |
| 58 | +(background flip-flop-chain |
| 59 | + [factory (chain-factory :start-elements [#t("flip" 1 100)] |
| 60 | + :elements {"flip" [#t("flop" 1 100)] |
| 61 | + "flop" [#t("flip" 1 100)]})] |
| 62 | + [chain (factory)]) |
| 63 | + |
| 64 | +(fact "markov chain can switch between states" |
| 65 | + (with-background flip-flop-chain [chain] |
| 66 | + (assert-that (first chain) (is- (equal-to "flip"))) |
| 67 | + (assert-that (first chain) (is- (equal-to "flop"))) |
| 68 | + (assert-that (first chain) (is- (equal-to "flip"))))) |
| 69 | + |
| 70 | +(background foo-bar-baz |
| 71 | + [factory (chain-factory :start-elements [#t("foo" 1 33) |
| 72 | + #t("bar" 34 66) |
| 73 | + #t("baz" 67 100)] |
| 74 | + :elements {"foo" [#t("bar" 1 50) |
| 75 | + #t("baz" 51 100)] |
| 76 | + "bar" [#t("foo" 1 50) |
| 77 | + #t("baz" 51 100)] |
| 78 | + "baz" [#t("foo" 1 50) |
| 79 | + #t("bar" 51 100)]})] |
| 80 | + [chain (factory)]) |
| 81 | + |
| 82 | +(fact "markov chain can have multiple transitions from single element" |
| 83 | + (with-background foo-bar-baz [chain] |
| 84 | + (assert-that (take 500 chain) |
| 85 | + (has-items "foo" "bar" "baz")))) |
0 commit comments