zoukankan      html  css  js  c++  java
  • Junit3.8 Stack测试

      1 package test;
      2 
      3 public class MyStack
      4 {
      5     private String[] elements;
      6     
      7     private int nextIndex;
      8     
      9     public MyStack()
     10     {
     11         elements = new String[100];
     12         nextIndex = 0;
     13     }
     14     
     15     public void push(String element) throws Exception
     16     {
     17         if(100 == nextIndex)
     18         {
     19             throw new Exception("数组越界异常!");
     20         }
     21         
     22         elements[nextIndex++] = element;
     23     }
     24     
     25     public String pop() throws Exception
     26     {
     27         if(0 == nextIndex)
     28         {
     29             throw new Exception("数组越界异常!");
     30         }
     31         
     32         return elements[--nextIndex];
     33     }
     34     
     35     public void delete(int n) throws Exception
     36     {
     37         if(nextIndex - n < 0)
     38         {
     39             throw new Exception("数组越界异常!");
     40         }
     41         
     42         nextIndex -= n;
     43     }
     44     
     45     public String top() throws Exception
     46     {
     47         if(0 == nextIndex)
     48         {
     49             throw new Exception("数组越界异常!");
     50         }
     51         
     52         return elements[nextIndex - 1];
     53     }
     54     
     55 }
     56 
     57 package junit;
     58 
     59 import junit.framework.Assert;
     60 import junit.framework.TestCase;
     61 
     62 @SuppressWarnings("deprecation")
     63 public class MyStackTest extends TestCase
     64 {
     65     private MyStack myStack;
     66 
     67     @Override
     68     public void setUp() throws Exception
     69     {
     70         myStack = new MyStack();
     71     }
     72     
     73     //测试正常情况的一般情况,向栈内放入一个元素,然后取出。
     74     public void testPush()
     75     {
     76         try
     77         {
     78             myStack.push("hello world");
     79         }
     80         catch (Exception e)
     81         {
     82             Assert.fail();
     83         }
     84 
     85         String result = null;
     86 
     87         try
     88         {
     89             result = myStack.pop();
     90         }
     91         catch (Exception ex)
     92         {
     93 
     94         }
     95 
     96         Assert.assertEquals("hello world", result);
     97     }
     98 
     99     //测试正常情况的临界情况,即放入100个元素栈是否会出错
    100     public void testPush2()
    101     {
    102         for (int i = 0; i < 100; i++)
    103         {
    104             try
    105             {
    106                 myStack.push(i + "");
    107             }
    108             catch (Exception ex)
    109             {
    110                 Assert.fail();
    111             }
    112         }
    113 
    114         for (int i = 0; i < 100; i++)
    115         {
    116             String result = null;
    117 
    118             try
    119             {
    120                 result = myStack.pop();
    121             }
    122             catch (Exception ex)
    123             {
    124 
    125             }
    126 
    127             Assert.assertEquals((99 - i) + "", result);
    128         }
    129     }
    130     
    131     //测试异常情况,101个元素输入
    132     public void testPush3()
    133     {
    134         Exception tx = null;
    135 
    136         try
    137         {
    138             for (int i = 0; i <= 100; i++)
    139             {
    140                 myStack.push(i + "");
    141             }
    142 
    143             Assert.fail();
    144         }
    145         catch (Exception ex)
    146         {
    147             tx = ex;
    148         }
    149 
    150         assertData(tx);
    151     }
    152 
    153     public void testPop()
    154     {
    155         try
    156         {
    157             myStack.push("hello world");
    158         }
    159         catch (Exception e)
    160         {
    161 
    162         }
    163 
    164         String result = null;
    165 
    166         try
    167         {
    168             result = myStack.pop();
    169         }
    170         catch (Exception ex)
    171         {
    172             Assert.fail();
    173         }
    174 
    175         Assert.assertEquals("hello world", result);
    176     }
    177 
    178     public void testPop2()
    179     {
    180         Throwable tx = null;
    181 
    182         try
    183         {
    184             myStack.pop();
    185 
    186             Assert.fail();
    187         }
    188         catch (Exception ex)
    189         {
    190             tx = ex;
    191         }
    192 
    193         assertData(tx);
    194     }
    195 
    196     public void testPop3()
    197     {
    198         Throwable tx = null;
    199 
    200         try
    201         {
    202             myStack.push("hello");
    203         }
    204         catch (Exception ex)
    205         {
    206 
    207         }
    208 
    209         try
    210         {
    211             myStack.pop();
    212             myStack.pop();
    213 
    214             Assert.fail();
    215         }
    216         catch (Exception ex)
    217         {
    218             tx = ex;
    219         }
    220 
    221         assertData(tx);
    222     }
    223 
    224     public void testTop()
    225     {
    226         try
    227         {
    228             myStack.push("hello");
    229         }
    230         catch (Exception ex)
    231         {
    232 
    233         }
    234 
    235         String result = null;
    236 
    237         try
    238         {
    239             result = myStack.top();
    240         }
    241         catch (Exception ex)
    242         {
    243             Assert.fail();
    244         }
    245 
    246         Assert.assertEquals("hello", result);
    247     }
    248 
    249     public void testTop2()
    250     {
    251         Throwable tx = null;
    252 
    253         try
    254         {
    255             myStack.top();
    256 
    257             Assert.fail();
    258         }
    259         catch (Exception ex)
    260         {
    261             tx = ex;
    262         }
    263 
    264         assertData(tx);
    265     }
    266 
    267     public void testDelete()
    268     {
    269         try
    270         {
    271             for (int i = 0; i < 10; i++)
    272             {
    273                 myStack.push(i + "");
    274             }
    275 
    276             myStack.delete(10);
    277         }
    278         catch (Exception ex)
    279         {
    280             Assert.fail();
    281         }
    282     }
    283 
    284     public void testDelete2()
    285     {
    286         Throwable tx = null;
    287 
    288         try
    289         {
    290             for (int i = 0; i < 10; i++)
    291             {
    292                 myStack.push(i + "");
    293             }
    294 
    295             myStack.delete(11);
    296 
    297             Assert.fail();
    298         }
    299         catch (Exception ex)
    300         {
    301             tx = ex;
    302         }
    303 
    304         assertData(tx);
    305     }
    306 
    307     private void assertData(Throwable tx)
    308     {
    309         Assert.assertNotNull(tx);
    310         Assert.assertEquals(Exception.class, tx.getClass());
    311         Assert.assertEquals("数组越界异常!", tx.getMessage());
    312     }
    313 
    314 }
  • 相关阅读:
    ORACLE SEQUENCE 介绍
    cocos2d 游戏开发:Cocos2d v3 &quot;hello world&quot;+显示飞船
    无线网络覆盖
    解决xShell4某些情况下按删除键会输出^H的问题
    Android开发经验之—intent传递大数据
    简单的REST的框架实现
    ListView 使用方法(Asp.Net)
    POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】
    C#反射Assembly 具体说明
    HDU 4432 Sum of divisors (进制模拟)
  • 原文地址:https://www.cnblogs.com/daneres/p/4688385.html
Copyright © 2011-2022 走看看