コード例 #1
0
 static void Main(string[] args)
 {
     Console.WriteLine("------------- Program start -----------");
     int[]       randomPrime = new int[NUMBER_OF_OBJECTS];
     NearPrime[] obj         = new NearPrime[NUMBER_OF_OBJECTS];
     InitializeRandomTwoDigitsPrime(randomPrime);
     InitializeNearPrimeObject(obj, randomPrime);
     for (int i = 0; i < NUMBER_OF_VALID_PRIMES; i++)
     {
         Console.WriteLine("======== Test Object {0} =========", i + 1);
         Console.WriteLine("Obj{0} is encapsulated a valid two digits" +
                           "prime number", i + 1);
         SimulateObjects(obj[i], i);
         Console.WriteLine();
     }
     for (int i = NUMBER_OF_VALID_PRIMES; i < NUMBER_OF_OBJECTS; i++)
     {
         Console.WriteLine("======== Test Object {0} =========", i + 1);
         Console.WriteLine("Obj{0} is encapsulated an invalid two digits" +
                           " prime number", i + 1);
         SimulateObjects(obj[i], i);
         Console.WriteLine();
     }
     Console.WriteLine("------------- Program stop -----------");
 }
コード例 #2
0
 public static void TestReviveWithActiveObject(NearPrime obj, int name,
                                               int queries)
 {
     while (obj.getActive())
     {
         Console.WriteLine("obj{0} is currently active -- " +
                           "Trying to revive", name + 1);
         obj.Revive();
     }
     Console.WriteLine("x     obj{0}.mod(x)", name + 1);
     for (int i = minNumberValidInput; i <= minNumberValidInput + queries;
          i++)
     {
         if (obj.getActive())
         {
             Console.WriteLine(i + "         " + obj.Mod(i) + "  ");
         }
         else
         {
             Console.WriteLine("Obj{0} is permanently deactivated",
                               name + 1);
             break;
         }
     }
 }
コード例 #3
0
        public static void SimulateObjects(NearPrime obj, int name)
        {
            Random random = new Random();

            Console.WriteLine("Testing object with valid input");
            Console.WriteLine("x     obj{0}.mod(x)", name + 1);
            int queries = random.Next(minQueries, maxQueries);

            TestModFunctionWithValidInput(obj, queries);

            Console.WriteLine();
            Console.WriteLine("Object is reset...");
            obj.Reset();
            Console.WriteLine();

            Console.WriteLine("Testing object with invalid input");
            Console.WriteLine("x     obj{0}.mod(x)", name + 1);
            TestModFunctionWithInValidInput(obj, queries);

            Console.WriteLine();
            Console.WriteLine("Object is reset...");
            obj.Reset();
            Console.WriteLine();
            Console.WriteLine("Testing revive active object");
            TestReviveWithActiveObject(obj, name, queries);
        }
コード例 #4
0
 public static void TestModFunctionWithInValidInput(NearPrime obj,
                                                    int queries)
 {
     for (int i = minNumberValidInput - queries; i < minNumberValidInput;
          i++)
     {
         if (obj.getActive())
         {
             Console.WriteLine(i + "         " + obj.Mod(i) + "  ");
         }
         else
         {
             Console.WriteLine("Input value is not valid -- Reviving");
             obj.Revive();
             break;
         }
     }
 }
コード例 #5
0
        public static void InitializeNearPrimeObject(NearPrime[] obj,
                                                     int[] randomPrime)
        {
            Console.WriteLine("Initialize nearPrime objects");
            for (int i = 0; i < NUMBER_OF_VALID_PRIMES; i++)
            {
                obj[i] = new NearPrime(randomPrime[i]);
            }
            Random random = new Random();

            for (int i = NUMBER_OF_VALID_PRIMES; i < NUMBER_OF_OBJECTS; i++)
            {
                int randomNumber = random.Next(-10, 110);
                while (randomNumber >= 11 && randomNumber <= 97)
                {
                    randomNumber = random.Next(-10, 110);
                }
                obj[i] = new NearPrime(randomNumber);
            }
        }
コード例 #6
0
        public static void TestModFunctionWithValidInput(NearPrime obj,
                                                         int queries)
        {
            int value = 0;

            for (int i = minNumberValidInput; i <= minNumberValidInput +
                 queries; i++)
            {
                if (obj.getActive())
                {
                    value = obj.Mod(i);
                }
                if (value != 0)
                {
                    Console.WriteLine(i + "        " + value);
                }
                else
                {
                    Console.WriteLine("Exceeded the query limit -- Reviving");
                    obj.Revive();
                    break;
                }
            }
        }