Question: Write unit tests for following 4 functions . /********************************** GCD finds the Greatest Common Divisor between 2 numbers. Write at least 3 tests to verify
Write unit tests for following 4 functions .
/********************************** GCD finds the Greatest Common Divisor between 2 numbers. Write at least 3 tests to verify the function is correct. **********************************/ class GCD { fun gcd(a: Int, b: Int): Int { var answer: Int when { (b == 0) -> answer = a else -> return gcd(b, a % b) } return answer } } /********************************** Hanoi solves the Towers of Hanoi problem for the specified number of disks and returns the total number of moves. Write at least 3 tests using getMoves() to verify the function is correct. **********************************/ class Hanoi(disks: Int) { private var moves = 0 init { println("Towers of Hanoi with $disks disks: ") move(disks, 'L', 'C', 'R') println(" Completed in $moves moves ") } private fun move(n: Int, from: Char, to: Char, via: Char):Int { if (n > 0) { move(n - 1, from, via, to) moves++ println("Move disk $n from $from to $to") move(n - 1, via, to, from) } return moves } fun getMoves(): Int { return moves } } /********************************** Factorial finds the factorial solution to the given number. Write at least 3 tests to verify all functionality is correct. **********************************/ class Factorial { fun fact(n: Int): Long { var result: Long = when { n < 0 -> -1 n < 2 -> 1 else -> n * fact(n-1) } return result } } /********************************** ArrayFunc contains 3 functions for use with an array of Ints: Sum, Mean, and Median. Write at least 4 tests to verify all functionality is correct. **********************************/ class ArrayFunc { fun getSum(vararg nums: Int): Int { var sum = 0 for (num in nums) { sum += num } return sum } fun getMean(vararg nums: Int): Double { return getSum(*nums).toDouble()/nums.size } fun getMedian(vararg nums: Int): Int { val sortedAry = nums.sorted() val median: Int if (sortedAry.size % 2 == 1) { median = sortedAry[Math.floor(sortedAry.size.toDouble() / 2).toInt()] } else { median = getMean(sortedAry[Math.floor(sortedAry.size.toDouble() / 2).toInt()], sortedAry[(Math.floor(sortedAry.size.toDouble() / 2).toInt()-1)]).toInt() } return median } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
