import junit.framework.TestCase;
import java.io.*;
public class StringTriviaTester extends TestCase {
  
  String empty, nonEmpty, vowels, consonants, moreVowels, moreConsonants;
  
  public StringTriviaTester(String methodName) {
    super(methodName);
  }
  
  public StringTriviaTester() {
  }
  
  /**
   * set up some strings and files for use in testing.
   */
  public void setUp() throws IOException {
    empty= "";
    nonEmpty= "X";
    vowels= "aeiouy";
    consonants= "bcdFghJklmnpq";
    moreVowels= "abXeyi";
    moreConsonants= "abXeyiQJhl";
    PrintStream out=
      new PrintStream(new FileOutputStream("zeroText.txt"));
    out.println("");
    out.close();
    out= new PrintStream(new FileOutputStream("alphaText.txt"));
    out.println("abcdefghijklmnopqrstuvwxyz");
    out.close();
    out= new PrintStream(new FileOutputStream("alphaZeroText.txt"));
    out.println("abcdefghijklmnopqrstuvwxy");
    out.println("");
    out.close();
  }
  
  
  /**
   * Check that there are not more vowels than consonants in the empty String
   */
  public void testVowelsBeatConsonantsEmptyString() {
    assertFalse("Empty String:",
                StringTrivia.vowelsBeatConsonants(empty));
  }
  
  /**
   * Check that there are more vowels than consonants in a String containing only vowels
   */
  public void testVowelsBeatConsonantsVowelsOnly() {
    assertTrue("All vowels: " + vowels,
               StringTrivia.vowelsBeatConsonants(vowels));
  }
  
  /**
   * Check that there are not more vowels than consonants in a String containing
   * only consonants.
   */
  public void testVowelsBeatConsonantsConsonantsOnly() {
    assertFalse("All consonants: " + consonants,
                StringTrivia.vowelsBeatConsonants(consonants));
  }
  
  /**
   * Check that there are more vowels than consonants in a mixed String with more
   * vowels.
   */
  public void testVowelsBeatConsonantsMoreVowels() {
    assertTrue("More vowels: " + moreVowels,
               StringTrivia.vowelsBeatConsonants(moreVowels));
  }
  
  /**
   * Check that there are not more vowels than consonants in a mixed String with
   * more consonants.
   */
  public void testVowelsBeatConsonantsMoreConsonants() {
    assertFalse("More consonants: " + moreConsonants,
                StringTrivia.vowelsBeatConsonants(moreConsonants));
  }
  
  /**
   * Check that the empty String is a subsequence of itself.
   */
  public void testIsSubsequenceEmptyEmpty() {
    assertTrue("Empty String subsequence of all Strings.",
               StringTrivia.isSubsequence(empty, empty));
  }
  
  /**
   * Check that the empty String is a subsequence of any non-empty String,
   * and that a non-empty String is not a subsequence of the empty String.
   */
  public void testIsSubsequenceEmptyNonEmpty() {
    assertTrue("Empty String subsequence of all Strings.",
               StringTrivia.isSubsequence(empty, nonEmpty));
    assertFalse("Nonempty String is not a subsequence of the empty String.",
                StringTrivia.isSubsequence(nonEmpty, empty));
  }
  
  /**
   * Check that a subsequence is sometimes not a substring, and that
   * transposing characters may destroy a subsequence.
   */
  public void testIsSubsequenceNonEmptyNonEmpty() {
    String s1= "ankle", s2= "FaQrnnTpkaalRRe", s3= "anlke";
    assertTrue("First String: " + s1 + " in: " + s2,
               StringTrivia.isSubsequence(s1, s2));
    assertFalse("First String: " + s1 + " not in: " + s3,
                StringTrivia.isSubsequence(s1, s3));
  }
  
  /**
   * Check that there are zero double letters in the empty String.
   */
  public void testDoubleLettersEmpty() {
    assertEquals("Empty String has no double letters.",
               0, StringTrivia.doubleLetters(empty));
  }
  
  /**
   * Check that there are zero double letters in a 1-character String.
   */
  public void testDoubleLettersNonEmpty() {
    assertEquals("Single-character String has no double letters: " + nonEmpty,
                 0, StringTrivia.doubleLetters(nonEmpty));
  }
  
  /**
   * Check that "ooo" has one non-overlapping pair of letters.
   */
  public void testDoubleLettersTriple() {
    assertEquals("One non-overlapping pair: ooo",
                 1, StringTrivia.doubleLetters("ooo"));
  }
  
  /**
   * Check that "bbbb" has two non-overlapping pairs of letters.
   */
  public void testDoubleLettersQuadruple() {
    assertEquals("Two non-overlapping pairs: bbbb",
                 2, StringTrivia.doubleLetters("bbbb"));
  }
  
  /**
   * Check that "bookkeepeer" (sp?!!?) has 4 non-overlapping pairs of letters.
   */
  public void testDoubleLettersLong() {
    assertEquals("Four non-overlapping pairs: bookkeepeer",
                 4, StringTrivia.doubleLetters("bookkeepeer"));
  }
  
  /**
   * Check that a file with a single empty line has average line length 0
   */
  public void testAverageStringLengthEmptyLine() throws IOException{
    assertEquals("Single empty line has average length zero.",
                 0.0, StringTrivia.averageStringLength("zeroText.txt"), 
                 0.000000001);
  }
  
  /**
   * Check that a file with a single non-empty line has average line length
   * equal to the number of characters in that line.
   */
  public void testAverageStringLengthAlphabetLine() throws IOException {
    assertEquals("Single 26-character line has average length 26.",
                 26.0, StringTrivia.averageStringLength("alphaText.txt"),
                 0.000000001);
  }
  
  /**
   * Check that average length of an empty line and a line with 25
   * characters is 12.5
   */
  public void testAverageStringLengthAlphaEmptyLine() throws IOException {
    assertEquals("25-character line and 0-character average to 12.5",
                 12.5, StringTrivia.averageStringLength("alphaZeroText.txt"),
                 0.000000001);
  }

}
