Finish ^deliberation code and tests.
parent
e67ae2c12d
commit
bd2bb2646d
@ -1,16 +1,94 @@
|
||||
(use-modules (deliberate)
|
||||
(goblins)
|
||||
(goblins vrun)
|
||||
(goblins actor-lib methods))
|
||||
;;; Copyright 2022 Edward L. Platt
|
||||
;;;
|
||||
;;; Licensed under the Apache License, Version 2.0 (the "License");
|
||||
;;; you may not use this file except in compliance with the License.
|
||||
;;; You may obtain a copy of the License at
|
||||
;;;
|
||||
;;; http://www.apache.org/licenses/LICENSE-2.0
|
||||
;;;
|
||||
;;; Unless required by applicable law or agreed to in writing, software
|
||||
;;; distributed under the License is distributed on an "AS IS" BASIS,
|
||||
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
;;; See the License for the specific language governing permissions and
|
||||
;;; limitations under the License.
|
||||
|
||||
(define-module (tests test-preference)
|
||||
#:use-module (srfi srfi-64)
|
||||
#:use-module (goblins)
|
||||
#:use-module (goblins vrun)
|
||||
#:use-module (goblins actor-lib methods)
|
||||
#:use-module (deliberate))
|
||||
|
||||
(define a-vat (spawn-vat))
|
||||
(define-vat-run a-run a-vat)
|
||||
(define delib (a-run (spawn ^deliberation "Bike Shed")))
|
||||
(a-run ($ delib 'add-alternative "red"))
|
||||
(a-run ($ delib 'add-alternative "green"))
|
||||
(a-run ($ delib 'add-alternative "blue"))
|
||||
(define vote (a-run (spawn ^ranked-choice-vote delib ($ delib 'debug-alternatives))))
|
||||
|
||||
(test-begin "test-deliberate")
|
||||
|
||||
;; Tests for ^deliberation-info
|
||||
(define info (a-run (spawn ^deliberation-info "Power Generator")))
|
||||
|
||||
(test-equal "Power Generator"
|
||||
(a-run ($ info 'title)))
|
||||
|
||||
(a-run ($ info 'set-title "Energy"))
|
||||
(test-equal "Energy"
|
||||
(a-run ($ info 'title)))
|
||||
|
||||
;; Tests for ^deliberation
|
||||
(define delib (a-run (spawn ^deliberation "Power Generator")))
|
||||
|
||||
(test-equal "Power Generator"
|
||||
(a-run ($ delib 'title)))
|
||||
|
||||
;; Tests for ^ranked-choice-vote
|
||||
(define vote (a-run ($ delib 'add-vote "What type of generator?")))
|
||||
|
||||
(a-run ($ delib 'votes))
|
||||
(a-run ($ vote 'ballots))
|
||||
|
||||
;; Add alternatives
|
||||
(a-run ($ vote 'add-alternative "solar"))
|
||||
(a-run ($ vote 'add-alternative "wind"))
|
||||
(a-run ($ vote 'add-alternative "hydro"))
|
||||
|
||||
;; Tests for ^ranked-ballot
|
||||
(define ballot (a-run ($ vote 'add-ballot)))
|
||||
|
||||
(define (rank31f alts) '(3 1 #f))
|
||||
(a-run ($ ballot 'update-vote rank31f))
|
||||
(a-run ($ vote 'ballots))
|
||||
(a-run ($ ballot 'alternatives))
|
||||
(a-run ($ ballot 'question))
|
||||
|
||||
;; Example ranking functions
|
||||
(define (f-wind-solar-hydro alts) '(3 1 2))
|
||||
(define (f-hydro-solar-wind alts) '(1 3 2))
|
||||
(define (f-solar-wind-hydro alts) '(3 2 1))
|
||||
|
||||
;; Cast a single vote
|
||||
(a-run ($ ballot 'update-vote f-wind-solar-hydro))
|
||||
|
||||
(test-equal '("wind" "solar" "hydro")
|
||||
(a-run ($ ballot 'ranking)))
|
||||
|
||||
|
||||
;; Add more votes to test voting methods
|
||||
|
||||
(let ((ballot (a-run ($ vote 'add-ballot))))
|
||||
(a-run ($ ballot 'update-vote f-wind-solar-hydro)))
|
||||
|
||||
(let ((ballot (a-run ($ vote 'add-ballot))))
|
||||
(a-run ($ ballot 'update-vote f-hydro-solar-wind)))
|
||||
(let ((ballot (a-run ($ vote 'add-ballot))))
|
||||
(a-run ($ ballot 'update-vote f-hydro-solar-wind)))
|
||||
|
||||
(let ((ballot (a-run ($ vote 'add-ballot))))
|
||||
(a-run ($ ballot 'update-vote f-solar-wind-hydro)))
|
||||
|
||||
;; Test instant-runoff
|
||||
(test-equal '("wind")
|
||||
(a-run ($ vote 'irv-winner)))
|
||||
|
||||
;; Test Borda method
|
||||
(test-equal '("solar")
|
||||
(a-run ($ vote 'borda-winner)))
|
||||
|
||||
(test-end "test-deliberate")
|
||||
|
Loading…
Reference in New Issue