From 8e361225aeb93d771d79eb0933ba87b58f518bbc Mon Sep 17 00:00:00 2001 From: Erik Rauer Date: Mon, 29 Mar 2021 21:01:27 -0500 Subject: [PATCH] Add test for `string/nth` This adds a test for the `string/nth` instruction --- .../push/instructions/string_spec.clj | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/propeller/push/instructions/string_spec.clj b/test/propeller/push/instructions/string_spec.clj index 622f530..4f32de4 100644 --- a/test/propeller/push/instructions/string_spec.clj +++ b/test/propeller/push/instructions/string_spec.clj @@ -266,4 +266,26 @@ (defspec length-spec 100 (prop/for-all [str gen/string] - (check-length str))) \ No newline at end of file + (check-length str))) + + +;; string/nth + +(defn check-nth + [value n] + (let [start-state (-> state/empty-state + (state/push-to-stack :string value) + (state/push-to-stack :integer n)) + end-state ((:string_nth @core/instruction-table) start-state)] + (or + (and (empty? value) + (state/empty-stack? end-state :char) + (= value (state/peek-stack end-state :string)) + (= n (state/peek-stack end-state :integer))) + (= (nth value (mod n (count value))) + (state/peek-stack end-state :char))))) + +(defspec nth-spec 100 + (prop/for-all [str gen/string + int gen/small-integer] + (check-nth str int))) \ No newline at end of file