• 软件测试技术第三次作业——打印质数printPrimes()


    作业一.Use the following method printPrimes() for questions a–d.

    printPrimes:

     /** *****************************************************
     * Finds and prints n prime integers
     * Jeff Offutt, Spring 2003
     ********************************************************* */
     private static void printPrimes (int n)
     {
     int curPrime; // Value currently considered for primeness
     int numPrimes; // Number of primes found so far.

    boolean isPrime; // Is curPrime prime? int [] primes = new int [MAXPRIMES]; // The list of prime numbers. // Initialize 2 into the list of primes. primes [0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n) { curPrime++; // next number to consider ... isPrime = true; for (int i = 0; i <= numPrimes-1; i++) { // for each previous prime. if (isDivisible (primes[i], curPrime)) { // Found a divisor, curPrime is not prime. isPrime = false; break; // out of loop through primes. } } if (isPrime) { // save it! primes[numPrimes] = curPrime; numPrimes++; } } // End while // Print all the primes out. for (int i = 0; i <= numPrimes-1; i++) { System.out.println ("Prime: " + primes[i]); } } // end printPrimes

    问题:(a) Draw the control ow graph for the printPrimes() method.

    //为printPrimes方法画控制流图:

    (b) Considertestcasest1=(n=3)andt2=(n=5).Although these tourthe same prime paths in printPrimes(), they do not necessarily find the same faults.Designasimplefaultthat t2 would bemorelikelytodiscover than t1 would.

    //什么情况下测试用例t2(n=5)会比t1(n=3)更有可能发现错误

    答:将MAXPRIMES设置为4时,t2会发生数组越界错误,但t1不会发生错误。

    (c) For printPrimes(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.

    //找一个测试用例,使得测试路径直接从while循环的开始跳到for循环而不经过while循环体

    答:当n=1时符合情况

    (d) Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().

    //枚举点覆盖,边覆盖,主路径覆盖

    答:

    点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

    边覆盖:{(1,2),(2,3),(3,4),(4,5),(5,6),(6,8),(8,5),(6,7),(7,9),(5,9),(9,10),(9,11),(10,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}

    主路径覆盖:{(1,2,3,4,5,6,8),(1,2,3,4,5,6,7,9,10,11),(1,2,3,4,5,6,7,9,11),(1,2,3,4,5,9,11),(1,2,3,4,5,9,10,11),(5,6,8,5),(6,8,5,6),(8,5,6,8),(8,5,6,7,9,11),(8,5,6,7,9,10,11),(1,2,12,13,16),(1,2,12,13,14,15),(13,14,15,13),(14,15,13,14),(15,13,14,15),(14,15,13,16),(15,13,16)}

    作业二:基于JunitEclemmajacoco)实现一个主路径覆盖的测试

    修改后的代码,可以直接运行:

    public class hw3 {
    
         public static void printPrimes (int n)
         {
         int curPrime; // Value currently considered for primeness
         int numPrimes; // Number of primes found so far.
    
         boolean isPrime; // Is curPrime prime?
         int MAXPRIMES = 10;
         int [] primes = new int [MAXPRIMES]; // The list of prime numbers.
         // Initialize 2 into the list of primes.
         primes [0] = 2;
         numPrimes = 1;
         curPrime = 2;
         while (numPrimes < n)
         {
         curPrime++; // next number to consider ...
         isPrime = true;
         for (int i = 0; i <= numPrimes-1; i++)
         { // for each previous prime.
         if (curPrime%primes[i]==0)  //此处原理:所有合数都能被一个小于它的质数整除
         { // Found a divisor, curPrime is not prime.
         isPrime = false;
         break; // out of loop through primes.
         }
         }
         if (isPrime)
         { // save it!
         primes[numPrimes] = curPrime;
         numPrimes++;
         }
         } // End while
         // Print all the primes out.
         for (int i = 0; i <= numPrimes-1; i++)
         {
         System.out.println ("Prime: " + primes[i]);
         }
         } // end printPrimes
         
         public static void main(String[] args){
             printPrimes(5);
         }
         
    }

    函数输出:

    Prime: 2
    Prime: 3
    Prime: 5
    Prime: 7
    Prime: 11

    利用JUnit生成测试:

    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class hw3Test {
    
        private hw3 test = new hw3();
        
        @Test
        public void testPrintPrimes() {
            test.printPrimes(5);
        }
    
    }

    结果:

    eclemma覆盖:

  • 相关阅读:
    SQLSERVER排查CPU占用高的情况
    bootstrap布局两列或者多列表单
    ASP.NET Redis 开发
    .NET 环境中使用RabbitMQ
    EasyNetQ介绍
    .Net使用RabbitMQ详解
    ASp.net中Froms验证方式
    全国-城市-百度地图中心点坐标
    IIS7.0下 HTTP 错误 404.15
    asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法
  • 原文地址:https://www.cnblogs.com/dhx96/p/6545154.html
Copyright © 2020-2023  润新知