'Using the testing tools, need to write a function that makes the test cases pass for IndexOf with a string and a character

I have the test cases that my teacher wants us to implement using testing tools available in Visual Studio:

str      c     Result
________________________

null    'a'   Exception
" "     'a'   -1
"a"     'b'   -1
"a"     'a'    0
"a..."  'b'   -1
"a..."  'a'    0
"ba.."  'b'    0
"a..b"  'b'    999,999,999

This is being done in C#. I have test methods written for 1-7. I have one for 8, but it's not quite right. Below is my code:

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTesting
{
    [TestClass]
    public class Helper
    {
        //Part 2 of Assignment
        public static int IndexOf(string s, char c)
        {
            while ()
            {
                return ...;
            }

            return -1;
        }

        //Test cases - part 3 of assignment
        //s = null, c = 'a', expected output = Exception
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestMethod1()
        {
            Helper.IndexOf(null, 'a');
        }

        //s = " ", c = 'a', expected outout = -1
        [TestMethod]
        public void TestMethod2()
        {
            Assert.AreEqual(-1, Helper.IndexOf("", 'a'));
        }

        //s = "a", c = 'b', expected outout = -1
        [TestMethod]
        public void TestMethod3()
        {
            Assert.AreEqual(-1, Helper.IndexOf("a", 'b'));
        }

        //s = "a", c = 'a', expected outout = 0
        [TestMethod]
        public void TestMethod4()
        {
            Assert.AreEqual(0, Helper.IndexOf("a", 'a'));
        }

        //s = "aaaaaa", c = 'b', expected outout = -1
        [TestMethod]
        public void TestMethod5()
        {
            Assert.AreEqual(-1, Helper.IndexOf("aaaaaa", 'b'));
        }

        //s = "aaaaaa", c = '', expected outout = 0
        [TestMethod]
        public void TestMethod6()
        {
            Assert.AreEqual(0, Helper.IndexOf("aaaaaa", 'a'));
        }

        //s = "baaaaa", c = 'b', expected outout = 0
        [TestMethod]
        public void TestMethod7()
        {
            Assert.AreEqual(0, Helper.IndexOf("baaaaa", 'b'));
        }

        ////s = "a...b", c = 'b', expected outout = 999999999
        //[TestMethod]
        //public void TestMethod8()
        //{
        //    string str = "aa";
        //    for (int i = 1; i <= 999999990; i++)
        //    {
        //        str.Insert(i, "a");
        //    }
        //    str.Insert(999999991, "b");
        //    Assert.AreEqual(999999991, Helper.IndexOf(str, 'b'));
        //}
    }
}

Test Method 8 is commented out because it is causing an out of range exception.

I have tried several things with the function. In class the teacher specifically mentioned a while loop. The instructions also want the return -1 in it for the results where the character is not found.

I can't figure out how to write the while loop. I tried using .Contains, the loop didn't end right. I have the return within the while loop because it seems to me something other than -1 needs to be returned for the results where the character is found.



Solution 1:[1]

No need for a loop at all, you can use

string str = new string('a', 99999999) + "b";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1