001 /** 002 * This is part of CSE 331 Problem Set 0. 003 */ 004 package ps0.test; 005 import ps0.RandomHello; 006 007 import junit.framework.TestCase; 008 import java.util.HashSet; 009 import java.util.Set; 010 011 /** 012 * RandomHelloTest is a simple test of the RandomHello class that is 013 * to be written by the students. This test just makes sure that the 014 * program does not crash and that it prints at least 5 different 015 * greetings. 016 */ 017 public class RandomHelloTest extends TestCase { 018 019 /** Number of times to run the greeting test until we're quite sure we'd have seen all the greetings */ 020 private int TIMES_TO_TEST = 1000; 021 022 /** Required number of greetings */ 023 private int REQUIRED_NUMBER_OF_GREETINGS = 5; 024 025 /** 026 * Tests that RandomHello does not crash. 027 */ 028 public void testCrash() { 029 /* If RandomHello.main() throws an exception, it will 030 * propagate outside testCrash() and JUnit will flag 031 * an error. */ 032 RandomHello.main(new String[0]); 033 } 034 035 036 /** 037 * Tests that the greetings are indeed random and that there are 038 * at least 5 different ones. 039 */ 040 public void testGreetings() { 041 RandomHello world = new RandomHello(); 042 Set<String> set = new HashSet<String>(); 043 044 for (int i=0; i< TIMES_TO_TEST; i++) { 045 String greeting = world.getGreeting(); 046 if (!set.contains(greeting)) { 047 set.add(greeting); 048 } 049 } 050 assertTrue("Insufficient number of greetings. Only "+set.size()+" greetings observed when we asked for "+ REQUIRED_NUMBER_OF_GREETINGS, set.size() >= REQUIRED_NUMBER_OF_GREETINGS); 051 assertFalse("Too many greetings in RandomHello. "+set.size()+" different greetings were observed when we asked for exactly "+ REQUIRED_NUMBER_OF_GREETINGS, set.size() > REQUIRED_NUMBER_OF_GREETINGS); 052 } 053 054 055 }