Skip to content

Commit 4268818

Browse files
committed
Add round, ceil, floor query terms
Add tests for round, ceil, and floor Add tests for lt, le, eq, ne, ge, gt Modify random test to set lower bound Fixes apa512#79
1 parent fa0460c commit 4268818

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/rethinkdb/query.cljc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,23 @@
607607
[n1 n2 & [optargs]]
608608
(term :RANDOM [n1 n2] optargs))
609609

610+
(defn round
611+
"Rounds the given value to the nearest whole integer.
612+
Less than n.5 rounds down, n.5 and greater rounds up."
613+
[n]
614+
(term :ROUND [n]))
615+
616+
(defn ceil
617+
"Rounds the given value up, returning the smallest integer value
618+
greater than or equal to the given value (the value’s ceiling)."
619+
[n]
620+
(term :CEIL [n]))
621+
622+
(defn floor
623+
"Rounds the given value down, returning the largest integer value less than or equal to the given value (the value’s floor)."
624+
[n]
625+
(term :FLOOR [n]))
626+
610627
;;; Dates and times
611628

612629
(defn now

test/rethinkdb/core_test.clj

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
(map (fn [[k v]] {k v}) m))
1313

1414
(def pokemons [{:national_no 25
15-
:name "Pikachu"
16-
:type ["Electric"]}
15+
:name "Pikachu"
16+
:type ["Electric"]}
1717
{:national_no 81
18-
:name "Magnemite"
19-
:type ["Electric" "Steel"]}])
18+
:name "Magnemite"
19+
:type ["Electric" "Steel"]}])
2020

2121
(def bulbasaur {:national_no 1
22-
:name "Bulbasaur"
23-
:type ["Grass" "Poison"]})
22+
:name "Bulbasaur"
23+
:type ["Grass" "Poison"]})
2424

2525
(defn ensure-table
2626
"Ensures that an empty table \"table-name\" exists"
@@ -220,11 +220,36 @@
220220

221221
(deftest math-and-logic
222222
(with-open [conn (r/connect)]
223-
(is (< (r/run (r/random 0 2) conn) 2))
223+
(is (<= 0 (r/run (r/random 0 2) conn) 2))
224224
(are [term result] (= (r/run term conn) result)
225225
(r/add 2 2 2) 6
226226
(r/add "Hello " "from " "Tokyo") "Hello from Tokyo"
227-
(r/add [1 2] [3 4]) [1 2 3 4])))
227+
(r/add [1 2] [3 4]) [1 2 3 4])
228+
229+
(are [args lt-le-eq-ne-ge-gt] (= (r/run (r/make-array
230+
(apply r/lt args)
231+
(apply r/le args)
232+
(apply r/eq args)
233+
(apply r/ne args)
234+
(apply r/ge args)
235+
(apply r/gt args)) conn)
236+
lt-le-eq-ne-ge-gt)
237+
[1 1] [false true true false true false]
238+
[0 1] [true true false true false false]
239+
[0 1 2 3] [true true false true false false]
240+
[0 1 1 2] [false true false true false false]
241+
[5 4 3] [false false false true true true]
242+
[5 4 4 3] [false false false true true false])
243+
244+
(are [n floor-round-ceil] (= (r/run (r/make-array (r/floor n) (r/round n) (r/ceil n)) conn) floor-round-ceil)
245+
0 [0 0 0]
246+
0.1 [0 0 1]
247+
1.499999999 [1 1 2]
248+
1.5 [1 2 2]
249+
1.5M [1 2 2]
250+
3.99999999 [3 4 4]
251+
-5.1 [-6 -5 -5]
252+
1/2 [0 1 1])))
228253

229254
(deftest geospatial-commands
230255
(with-open [conn (r/connect)]

0 commit comments

Comments
 (0)