Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220894
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-03-30 09:50:13

  1. /* Pragmatic Unit Testing(2): the first unit test
  2.    created on Mar 30, 2011
  3.    */
  4. public class Largest {
  5.     
  6.     public static int largest(int[] list) {
  7.         int index, max = Integer.MIN_VALUE; //try max = Integer.MAX_VALUE and max = 0
  8.         if(list.length == 0) {
  9.             throw new RuntimeException("Empty list");
  10.         }

  11.         for(index = 0; index < list.length; index++) { //try index < list.length-1
  12.             if(list[index] > max) {
  13.             max = list[index];
  14.          }
  15.      }
  16.         return max;
  17.     }
  18. }///:~

  19. /* A first test for Largest.largest() method
  20.    created on Mar 30, 2011
  21.    */

  22. import junit.framework.*;

  23. public class TestLargest extends TestCase {
  24.     public TestLargest(String name) {
  25.         super(name);
  26.     }

  27.     public void testSimple() {
  28.         assertEquals(9, Largest.largest(new int[] {7, 8, 9}));
  29.     }

  30.     public void testOrder() {
  31.         assertEquals(9, Largest.largest(new int[] {9, 7, 8}));
  32.         assertEquals(9, Largest.largest(new int[] {7, 9, 8}));
  33.     }

  34.     public void testDups() {
  35.         assertEquals(9, Largest.largest(new int[] {9, 9, 7, 8}));
  36.     }

  37.     public void testNegtive() {
  38.         int[] negList = new int[] {-9, -8, -7};
  39.         assertEquals(-7, Largest.largest(negList));
  40.     }

  41.     public void testOne() {
  42.         assertEquals(1, Largest.largest(new int[] {1}));
  43.     }

  44.     public void testEmpty() {
  45.         try{
  46.             Largest.largest(new int[] {});
  47.             fail("Should have thrown an exception ");
  48.         } catch (RuntimeException e) {
  49.             assertTrue(true);
  50.         }
  51.     }
  52. }
阅读(516) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~