Mercurial > lasercutter
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 and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; test-database.clj10 ;;11 ;; A Clojure implementation of Datalog -- Database12 ;;13 ;; straszheimjeffrey (gmail)14 ;; Created 12 Feburary 200917 (ns clojure.contrib.datalog.tests.test-database18 (:use clojure.test19 clojure.contrib.datalog.database))22 (def test-db23 (make-database24 (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-database31 (is (= test-db32 (datalog-database33 {:sally (datalog-relation34 #{:jen :joan :becky}35 #{}36 {:becky {}37 :jen {}})38 :fred (datalog-relation39 #{:sue :mary}40 #{}41 {:mary {}})}))))44 (deftest test-ensure-relation45 (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-tuple50 (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-155 (add-tuples test-db56 [: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-tuples66 (is (= test-db-167 (datalog-database68 {:sally (datalog-relation69 #{: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 {375 #{{:jen 1, :joan 0, :becky 3}}76 477 #{{:jen 1, :joan 3, :becky 4}}78 279 #{{:jen 1, :joan 0, :becky 2}80 {:jen 1, :joan 3, :becky 2}}}81 :jen {182 #{{: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-relation87 #{:sue :mary}88 #{{:sue 2, :mary 1}89 {:sue 1, :mary 1}90 {:sue 3, :mary 2}91 {:sue 1, :mary 3}}92 {:mary {393 #{{:sue 1, :mary 3}}94 295 #{{:sue 3, :mary 2}}96 197 #{{:sue 2, :mary 1}98 {:sue 1, :mary 1}}}})}))))100 (deftest test-remove-tuples101 (let [db (reduce #(apply remove-tuple %1 (first %2) (next %2))102 test-db-1103 [[: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 (= db108 (datalog-database109 {:sally (datalog-relation110 #{:jen :joan :becky}111 #{{:jen 1, :joan 0, :becky 3}112 {:jen 1, :joan 3, :becky 2}}113 {:becky114 {3115 #{{:jen 1, :joan 0, :becky 3}}116 2117 #{{:jen 1, :joan 3, :becky 2}}}118 :jen119 {1120 #{{:jen 1, :joan 0, :becky 3}121 {:jen 1, :joan 3, :becky 2}}}})122 :fred (datalog-relation123 #{:sue :mary}124 #{{:sue 2, :mary 1}125 {:sue 3, :mary 2}}126 {:mary127 {2128 #{{:sue 3, :mary 2}}129 1130 #{{:sue 2, :mary 1}}}})})))))134 (deftest test-select135 (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 (comment149 (run-tests)150 )152 ;; End of file