view src/clojure/contrib/test_contrib/datalog/tests/test_database.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and
2 ;; distribution terms for this software are covered by the Eclipse Public
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
4 ;; be found in the file epl-v10.html at the root of this distribution. By
5 ;; using this software in any fashion, you are agreeing to be bound by the
6 ;; terms of this license. You must not remove this notice, or any other,
7 ;; from this software.
8 ;;
9 ;; test-database.clj
10 ;;
11 ;; A Clojure implementation of Datalog -- Database
12 ;;
13 ;; straszheimjeffrey (gmail)
14 ;; Created 12 Feburary 2009
17 (ns clojure.contrib.datalog.tests.test-database
18 (:use clojure.test
19 clojure.contrib.datalog.database))
22 (def test-db
23 (make-database
24 (relation :fred [:mary :sue])
25 (index :fred :mary)
26 (relation :sally [:jen :becky :joan])
27 (index :sally :jen)
28 (index :sally :becky)))
30 (deftest test-make-database
31 (is (= test-db
32 (datalog-database
33 {:sally (datalog-relation
34 #{:jen :joan :becky}
35 #{}
36 {:becky {}
37 :jen {}})
38 :fred (datalog-relation
39 #{:sue :mary}
40 #{}
41 {:mary {}})}))))
44 (deftest test-ensure-relation
45 (is (contains? (ensure-relation test-db :bob [:sam :george] [:sam]) :bob))
46 (is (contains? (ensure-relation test-db :fred [:mary :sue] [:mary]) :fred))
47 (is (thrown? AssertionError (ensure-relation test-db :fred [:bob :joe] []))))
49 (deftest test-add-tuple
50 (let [new-db (add-tuple test-db :fred {:mary 1 :sue 2})]
51 (is (= (select new-db :fred {:mary 1}) [{:mary 1 :sue 2}])))
52 (is (thrown? AssertionError (add-tuple test-db :fred {:mary 1}))))
54 (def test-db-1
55 (add-tuples test-db
56 [:fred :mary 1 :sue 2]
57 [:fred :mary 2 :sue 3]
58 [:sally :jen 1 :becky 2 :joan 0]
59 [:sally :jen 1 :becky 4 :joan 3]
60 [:sally :jen 1 :becky 3 :joan 0]
61 [:sally :jen 1 :becky 2 :joan 3]
62 [:fred :mary 1 :sue 1]
63 [:fred :mary 3 :sue 1]))
65 (deftest test-add-tuples
66 (is (= test-db-1
67 (datalog-database
68 {:sally (datalog-relation
69 #{:jen :joan :becky}
70 #{{:jen 1, :joan 0, :becky 3}
71 {:jen 1, :joan 0, :becky 2}
72 {:jen 1, :joan 3, :becky 2}
73 {:jen 1, :joan 3, :becky 4}}
74 {:becky {3
75 #{{:jen 1, :joan 0, :becky 3}}
76 4
77 #{{:jen 1, :joan 3, :becky 4}}
78 2
79 #{{:jen 1, :joan 0, :becky 2}
80 {:jen 1, :joan 3, :becky 2}}}
81 :jen {1
82 #{{:jen 1, :joan 0, :becky 3}
83 {:jen 1, :joan 0, :becky 2}
84 {:jen 1, :joan 3, :becky 2}
85 {:jen 1, :joan 3, :becky 4}}}})
86 :fred (datalog-relation
87 #{:sue :mary}
88 #{{:sue 2, :mary 1}
89 {:sue 1, :mary 1}
90 {:sue 3, :mary 2}
91 {:sue 1, :mary 3}}
92 {:mary {3
93 #{{:sue 1, :mary 3}}
94 2
95 #{{:sue 3, :mary 2}}
96 1
97 #{{:sue 2, :mary 1}
98 {:sue 1, :mary 1}}}})}))))
100 (deftest test-remove-tuples
101 (let [db (reduce #(apply remove-tuple %1 (first %2) (next %2))
102 test-db-1
103 [[:fred {:mary 1 :sue 1}]
104 [:fred {:mary 3 :sue 1}]
105 [:sally {:jen 1 :becky 2 :joan 0}]
106 [:sally {:jen 1 :becky 4 :joan 3}]])]
107 (is (= db
108 (datalog-database
109 {:sally (datalog-relation
110 #{:jen :joan :becky}
111 #{{:jen 1, :joan 0, :becky 3}
112 {:jen 1, :joan 3, :becky 2}}
113 {:becky
114 {3
115 #{{:jen 1, :joan 0, :becky 3}}
116 2
117 #{{:jen 1, :joan 3, :becky 2}}}
118 :jen
119 {1
120 #{{:jen 1, :joan 0, :becky 3}
121 {:jen 1, :joan 3, :becky 2}}}})
122 :fred (datalog-relation
123 #{:sue :mary}
124 #{{:sue 2, :mary 1}
125 {:sue 3, :mary 2}}
126 {:mary
127 {2
128 #{{:sue 3, :mary 2}}
129 1
130 #{{:sue 2, :mary 1}}}})})))))
134 (deftest test-select
135 (is (= (set (select test-db-1 :sally {:jen 1 :becky 2}))
136 #{{:jen 1 :joan 0 :becky 2} {:jen 1 :joan 3 :becky 2}}))
137 (is (= (set (select test-db-1 :fred {:sue 1})))
138 #{{:mary 3 :sue 1} {:mary 1 :sue 1}})
139 (is (empty? (select test-db-1 :sally {:joan 5 :jen 1}))))
141 (deftest test-any-match?
142 (is (any-match? test-db-1 :fred {:mary 3}))
143 (is (any-match? test-db-1 :sally {:jen 1 :becky 2 :joan 3}))
144 (is (not (any-match? test-db-1 :sally {:jen 5})))
145 (is (not (any-match? test-db-1 :fred {:mary 1 :sue 5}))))
148 (comment
149 (run-tests)
150 )
152 ;; End of file