jeudi 25 avril 2019

Find digit at certain point passing all tests except one

I am writing test-driven code, and I have gotten all of my tests to pass except one test04 and I am not sure why it isn't passing. The failure message I am getting is epected<0> but was <1>, I don't understand why all of my other tests are passing and this one isn't. I have posted the method code and then the test methods that are being run on that method, and test04 is the one that keeps failing. Any help would be greatly appreciated.

Method Code:

 public static <T> int digitAt(int number, int place) {
            int exponent = place / 10;
            int digit = (int) (number / (Math.pow(10, exponent)) % 10);
            if (number < 0) {
                digit = digit / -1;
            }
            return digit;  
        }

Test Code:

    import static org.junit.Assert.*;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class Test
    {
        /* ---------- TESTS FOR digitAt ----------*/
        /* digitAt should take a number (int) and a 
         * "place" (1s, 10s, 100s, etc.) given as an int
         * and return the digit in the number at that place
         */

        @Test
        public void test01() {
            assertEquals(Lab9.digitAt(1023,1), 3);
        }

        @Test
        public void test02() {
            assertEquals(Lab9.digitAt(1023,10), 2);
        }

        @Test
        public void test03() {
            assertEquals(Lab9.digitAt(1023,100), 0);
        }

        @Test
        public void test04() {
            assertEquals(Lab9.digitAt(1023,1000), 1);
        }

        @Test
        public void test05() {
            assertEquals(Lab9.digitAt(1023,10000), 0);
        }

        @Test
        public void test06() {
            assertEquals(Lab9.digitAt(-1023,10), 2);
        }

        @Test
        public void test07() {
            assertEquals(Lab9.digitAt(-23,100), 0);
        }

Aucun commentaire:

Enregistrer un commentaire