Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution for simple calculator issue #448

Merged
merged 2 commits into from Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/battle_asserts/issues/simple_calculator.clj
@@ -0,0 +1,32 @@
(ns battle-asserts.issues.simple-calculator
(:require [clojure.test.check.generators :as gen]))

(def level :easy)

(def description "Create simple calculator that supports next operations: add, substract, divide, multiply, modulo.")

(def signature
{:input [{:argument-name "first-num" :type {:name "integer"}}
{:argument-name "second-num" :type {:name "integer"}}
{:argument-name "operation" :type {:name "string"}}]
:output {:type {:name "integer"}}})

(defn arguments-generator []
(let [operations ["+" "-" "*" "/" "%"]]
(gen/tuple (gen/choose -50 50) (gen/one-of [(gen/choose 1 50) (gen/choose -50 -1)]) (gen/elements operations))))

(def test-data
[{:expected 11 :arguments [1 10 "+"]}
{:expected -3 :arguments [2 5 "-"]}
{:expected 0 :arguments [0 5 "*"]}
{:expected 5 :arguments [10 2 "/"]}
{:expected 2 :arguments [2 3 "%"]}])

(defn solution
[first-num second-num operation]
(let [operations-map {"+" (fn [a b] (+ a b))
"-" (fn [a b] (- a b))
"*" (fn [a b] (* a b))
"/" (fn [a b] (/ a b))
"%" (fn [a b] (mod a b))}]
((operations-map operation) first-num second-num)))
15 changes: 15 additions & 0 deletions test/battle_asserts/issues/simple_calculator_test.clj
@@ -0,0 +1,15 @@
(ns battle-asserts.issues.simple-calculator-test
(:require [clojure.test :refer :all]
[clojure.test.check.properties :as prop]
[clojure.test.check.clojure-test :as ct]
[test-helper :as h]
[battle-asserts.issues.simple-calculator :as issue]))

(ct/defspec spec-solution
20
(prop/for-all [v (issue/arguments-generator)]
(instance? Number (apply issue/solution v))))

(deftest test-solution
(h/generate-tests issue/test-data issue/solution))