In cases where there is a possibility of going outside the int range, it's good to prevent it from returning the wrong product. Doesn't have to be an exception, but should be documented so other developers can tell what cases they have to take into account when they use your function. I'm for exceptions because it forces developers to deal with scenarios rather than just ignore the warnings in the documentation or to just not read the documentation.
If you know that you will never have the possibility of going out of the int's range, then this is overkill.
As for test cases I'm a proponent that test cases should only test one scenario. I find multiple asserts are fine, if you need to check multiple things to verify the scenario. If you need/want to test ranges or multiple values in one scenario, then refactor them into parameters for the test cases so the testing framework can do it's job and your test cases will be as simple as possible. If possible.
using System; public class Multiplication { // Multiply two integers // Throws error if the product is outside of the int range. // This would otherwise silently error and return the wrong product. public long Multiply(int a, int b) { long res = (long)a * (long)b; // make sure it's still a valid int if (res > Int32.MaxValue || res < Int32.MinValue) throw new InvalidOperationException("Product is outside range of int"); return res; } }
- using System;
- public class Multiplication
- {
public int Multip(int a, int b){return a * b;- // Multiply two integers
- // Throws error if the product is outside of the int range.
- // This would otherwise silently error and return the wrong product.
- public long Multiply(int a, int b) {
- long res = (long)a * (long)b;
- // make sure it's still a valid int
- if (res > Int32.MaxValue || res < Int32.MinValue)
- throw new InvalidOperationException("Product is outside range of int");
- return res;
- }
- }
namespace Solution { using NUnit.Framework; using System; [TestFixture] public class SolutionTest { // Test Happy Path [TestCase(-5,-1,5)] [TestCase(-5, 1,-5)] [TestCase(5,0,0)] [TestCase(5,5,25)] [TestCase(856659,5,4283295)] public void testValidMultiplication(int a, int b, int expected) { Multiplication test = new Multiplication(); long actual = test.Multiply(a, b); Assert.AreEqual(expected, actual); } // Test outside valid int ranges [TestCase(856659,-6523158)] [TestCase(856659,6523158)] [TestCase(Int32.MaxValue,Int32.MaxValue)] [TestCase(Int32.MaxValue,Int32.MinValue)] public void testInvalidIntMultiplication(int a, int b) { Multiplication test = new Multiplication(); long actual; Assert.Throws<InvalidOperationException>(() => actual = test.Multiply(a,b)); } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
[Test]public void MyTest()- // Test Happy Path
- [TestCase(-5,-1,5)]
- [TestCase(-5, 1,-5)]
- [TestCase(5,0,0)]
- [TestCase(5,5,25)]
- [TestCase(856659,5,4283295)]
- public void testValidMultiplication(int a, int b, int expected)
- {
int a = 10;int b = 5;int expected = 50;- Multiplication test = new Multiplication();
int actural = test.Multip(a, b);- long actual = test.Multiply(a, b);
Assert.AreEqual(expected, actural);- Assert.AreEqual(expected, actual);
- }
- // Test outside valid int ranges
- [TestCase(856659,-6523158)]
- [TestCase(856659,6523158)]
- [TestCase(Int32.MaxValue,Int32.MaxValue)]
- [TestCase(Int32.MaxValue,Int32.MinValue)]
- public void testInvalidIntMultiplication(int a, int b) {
- Multiplication test = new Multiplication();
- long actual;
- Assert.Throws<InvalidOperationException>(() => actual = test.Multiply(a,b));
- }
- }
- }