Corrected the test case and have written the code as well.