Go Testing Fundamentals: A Simple Calculator
This challenge introduces you to the basics of writing unit tests in Go. You'll be building a simple calculator and then writing tests to ensure its functionality is correct, covering both standard cases and potential edge cases. Understanding testing is crucial for writing robust and maintainable Go code.
Problem Description
You are tasked with creating a calculator package that provides basic arithmetic operations: Add, Subtract, Multiply, and Divide. Your goal is to implement these functions and then write a suite of unit tests using Go's built-in testing package to verify their correctness. The tests should cover a range of inputs, including positive numbers, negative numbers, zero, and potential division-by-zero scenarios.
What needs to be achieved:
- Create a
calculatorpackage with four functions:Add,Subtract,Multiply, andDivide. - Implement each function to perform the corresponding arithmetic operation.
- Write unit tests for each function within a
calculator_test.gofile. - Ensure the tests cover various input scenarios, including edge cases.
Key Requirements:
- The
calculatorpackage should be well-structured and easy to understand. - The unit tests should be clear, concise, and well-documented.
- The tests should accurately verify the behavior of the calculator functions.
- Handle division by zero gracefully (return an error).
Expected Behavior:
Add(a, b): Returns the sum ofaandb.Subtract(a, b): Returns the difference betweenaandb(a - b).Multiply(a, b): Returns the product ofaandb.Divide(a, b): Returns the quotient ofadivided byb. Ifbis zero, it should return 0 and an error.
Edge Cases to Consider:
- Division by zero.
- Negative numbers.
- Zero as an input.
- Large numbers (to check for potential overflow, though this is less critical for this exercise).
Examples
Example 1:
Input: calculator.Add(5, 3)
Output: 8
Explanation: 5 + 3 = 8
Example 2:
Input: calculator.Subtract(10, 4)
Output: 6
Explanation: 10 - 4 = 6
Example 3:
Input: calculator.Divide(10, 0)
Output: 0, error
Explanation: Division by zero results in an error. The function should return 0 and an error.
Example 4:
Input: calculator.Multiply(-2, 4)
Output: -8
Explanation: -2 * 4 = -8
Constraints
- All functions should accept
float64as input. - The
Dividefunction should return both afloat64result and anerror. - The error returned by
Dividewhen dividing by zero should be a meaningful error (e.g., "division by zero"). - Tests should use the
testingpackage and thet.Errorfort.Fatalffunctions for reporting errors. - The code should be idiomatic Go.
Notes
- Start by creating the
calculatorpackage and implementing the functions. - Then, create the
calculator_test.gofile and write your tests. - Use
go testto run your tests. - Consider using table-driven tests for a more concise and readable test suite. This involves defining a slice of test cases, each containing an input and the expected output.
- Focus on writing tests that are easy to understand and maintain. Good test names are crucial.
- Remember to handle the error case in the
Dividefunction correctly within your tests.