From 07ef1d3f9046ba44656e652797e52ac71d4067c0 Mon Sep 17 00:00:00 2001 From: Erik Rauer Date: Thu, 15 Apr 2021 16:03:57 -0500 Subject: [PATCH] Add tests for `string/substr` and `str/take` This adds tests for the `string/substr` and `str/take` instructions. --- .../push/instructions/string_spec.clj | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/propeller/push/instructions/string_spec.clj b/test/propeller/push/instructions/string_spec.clj index d5e6e99..11d0b78 100644 --- a/test/propeller/push/instructions/string_spec.clj +++ b/test/propeller/push/instructions/string_spec.clj @@ -507,4 +507,36 @@ (defspec split-spec 100 (prop/for-all [str gen/string] - (check-split str))) \ No newline at end of file + (check-split str))) + + +;; string/substr + +(defn check-substr + [instruction value start end] + (let [start-state (-> state/empty-state + (state/push-to-stack :string value) + (state/push-to-stack :integer start) + (state/push-to-stack :integer end)) + end-state ((instruction @core/instruction-table) start-state) + str-len (count value) + small (min str-len (max 0 start)) + big (min str-len (max 0 small end)) + expected-result (subs value small big)] + (= expected-result + (state/peek-stack end-state :string)))) + +(defspec substr-spec 100 + (prop/for-all + [tuple (gen/let [str gen/string + int1 (gen/large-integer* {:min -5 :max (+ 5 (count str))}) + int2 (gen/large-integer* {:min -5 :max (+ 5 (count str))})] + [:string_substr, str, int1, int2])] + (apply check-substr tuple))) + +(defspec take-spec 100 + (prop/for-all + [tuple (gen/let [str gen/string + int (gen/large-integer* {:min -5 :max (+ 5 (count str))})] + [:string_take, str, 0, int])] + (apply check-substr tuple))) \ No newline at end of file