Skip to content

Commit

Permalink
difference-of-squares: add generator and regenerate tests
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
ErikSchierboom committed Jan 12, 2025
1 parent 89aec85 commit e027b27
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 3 additions & 2 deletions exercises/practice/difference-of-squares/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns difference-of-squares)

(defn- square [n] (* n n))
(defn- sum [xs] (reduce + xs))

(defn sum-of-squares [n]
(sum (map #(int (Math/pow % 2)) (range 0 (inc n)))))
(sum (map square (range 1 (inc n)))))

(defn square-of-sum [n]
(int (Math/pow (sum (range 0 (inc n))) 2)))
(square (sum (range 1 (inc n)))))

(defn difference [x]
(- (square-of-sum x) (sum-of-squares x)))
7 changes: 7 additions & 0 deletions exercises/practice/difference-of-squares/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns difference-of-squares-generator)

(defn- update-path [path]
(take-last 1 path))

(defn transform [test-cases]
(map #(update % :path update-path) test-cases))
18 changes: 18 additions & 0 deletions exercises/practice/difference-of-squares/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns difference-of-squares-test
(:require [clojure.test :refer [deftest testing is]]
difference-of-squares))
{{#test_cases.squareOfSum}}
(deftest square-of-sum_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/square-of-sum {{input.number}})))))
{{/test_cases.squareOfSum~}}
{{#test_cases.sumOfSquares}}
(deftest sum-of-squares_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/sum-of-squares {{input.number}})))))
{{/test_cases.sumOfSquares~}}
{{#test_cases.differenceOfSquares}}
(deftest difference_test_{{idx}}
(testing "{{description}}"
(is (= {{expected}} (difference-of-squares/difference {{input.number}})))))
{{/test_cases.differenceOfSquares~}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
difference-of-squares))

(deftest square-of-sum_test_1
(testing "Square the sum of the numbers up to the given number -> square of sum 1"
(testing "square of sum 1"
(is (= 1 (difference-of-squares/square-of-sum 1)))))

(deftest square-of-sum_test_2
(testing "Square the sum of the numbers up to the given number -> square of sum 5"
(testing "square of sum 5"
(is (= 225 (difference-of-squares/square-of-sum 5)))))

(deftest square-of-sum_test_3
(testing "Square the sum of the numbers up to the given number -> square of sum 100"
(testing "square of sum 100"
(is (= 25502500 (difference-of-squares/square-of-sum 100)))))

(deftest sum-of-squares_test_1
(testing "Sum the squares of the numbers up to the given number -> sum of squares 1"
(testing "sum of squares 1"
(is (= 1 (difference-of-squares/sum-of-squares 1)))))

(deftest sum-of-squares_test_2
(testing "Sum the squares of the numbers up to the given number -> sum of squares 5"
(testing "sum of squares 5"
(is (= 55 (difference-of-squares/sum-of-squares 5)))))

(deftest sum-of-squares_test_3
(testing "Sum the squares of the numbers up to the given number -> sum of squares 100"
(testing "sum of squares 100"
(is (= 338350 (difference-of-squares/sum-of-squares 100)))))

(deftest difference_test_1
(testing "Subtract sum of squares from square of sums -> difference of squares 1"
(testing "difference of squares 1"
(is (= 0 (difference-of-squares/difference 1)))))

(deftest difference_test_2
(testing "Subtract sum of squares from square of sums -> difference of squares 5"
(testing "difference of squares 5"
(is (= 170 (difference-of-squares/difference 5)))))

(deftest difference_test_3
(testing "Subtract sum of squares from square of sums -> difference of squares 100"
(testing "difference of squares 100"
(is (= 25164150 (difference-of-squares/difference 100)))))

0 comments on commit e027b27

Please sign in to comment.