Last active
January 8, 2019 16:31
-
-
Save line-o/c06d1d7b8baf3ec4447c273f03726b54 to your computer and use it in GitHub Desktop.
Set of tests for filtering/ counting sequences of boolean values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
(:~ | |
these tests were created because of | |
https://github.com/eXist-db/exist/issues/2308 | |
~:) | |
module namespace boolseq="http://exist-db.org/xquery/xqsuite/boolseq"; | |
declare namespace test="http://exist-db.org/xquery/xqsuite"; | |
declare variable $boolseq:sequence := (true(), false(), true()); | |
(:~ | |
wrapping the not() function to | |
mitigate the issue with context-item handling in eXistdb | |
~:) | |
declare %private function boolseq:wrappedNot($a) { not($a) }; | |
declare | |
%test:assertEquals(3) | |
function boolseq:countAll() { | |
count($boolseq:sequence) | |
}; | |
declare | |
%test:assertEquals(2) | |
function boolseq:arrowCountPositivesContextItem() { | |
$boolseq:sequence[.] => count() | |
}; | |
declare | |
%test:assertEquals(1) | |
function boolseq:arrowCountNegativesContextItem() { | |
$boolseq:sequence[not(.)] => count() | |
}; | |
declare | |
%test:assertEquals(2) | |
function boolseq:countPositivesContextItem() { | |
count($boolseq:sequence[.]) | |
}; | |
(:~ | |
this is the failing issue | |
~:) | |
declare | |
%test:pending | |
%test:assertEquals(1) | |
function boolseq:countNegativesContextItem() { | |
count($boolseq:sequence[not(.)]) | |
}; | |
declare | |
%test:assertEquals(1) | |
function boolseq:countNegativesContextItemWrappedNot() { | |
count($boolseq:sequence[boolseq:wrappedNot(.)]) | |
}; | |
declare | |
%test:assertEquals(2) | |
function boolseq:countPositivesExplicitEquals() { | |
count($boolseq:sequence[. eq true()]) | |
}; | |
declare | |
%test:assertEquals(1) | |
function boolseq:countNegativesExplicitEquals() { | |
count($boolseq:sequence[. eq false()]) | |
}; | |
declare | |
%test:assertEquals(2) | |
function boolseq:countPositivesFold() { | |
fold-left($boolseq:sequence, 0, | |
function($r, $n) { if ($n) then ($r + 1) else ($r) }) | |
}; | |
declare | |
%test:assertEquals(1) | |
function boolseq:countNegativesFold() { | |
fold-left($boolseq:sequence, 0, | |
function($r, $n) { if (not($n)) then ($r + 1) else ($r) }) | |
}; | |
declare | |
%test:assertEquals(2) | |
function boolseq:countPositivesFilter() { | |
count( | |
filter( | |
$boolseq:sequence, function($n) { $n })) | |
}; | |
declare | |
%test:assertEquals(1) | |
function boolseq:countNegativesFilter() { | |
count( | |
filter( | |
$boolseq:sequence, function($n) { not($n) })) | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment