summaryrefslogtreecommitdiff
path: root/src/Q1/ReversedSentenceTest.java
blob: a7a398845cc6e52d79043a950b4392d711e15c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ca.mokhan.test;

import Q1.*;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ReversedSentenceTest extends TestCase {
  private ReversedSentence subject;

  public ReversedSentenceTest(String testName) {
    super(testName);
    this.subject = new ReversedSentence();
  }

  public static Test suite() {
    return new TestSuite(ReversedSentenceTest.class);
  }

  public void testTruthy() {
    assertEquals(true, true);
  }

  public void testReverseByCharacter() {
    assertEquals(
        "bmal elttil a dah yram", ReversedSentence.reverseByCharacter("mary had a little lamb"));
  }

  public void testReverseByWord() {
    assertEquals(
        "lamb little a had mary", ReversedSentence.reverseByWord("mary had a little lamb"));
  }

  public void testChange5thPosition() {
    assertEquals(
        "mary zad azlittze lazb", ReversedSentence.change5thPosition("mary had a little lamb"));
  }

  public void testPrintChar2DArray() {
    char[][] matrix = new char[3][80];
    String sentence = "mary had a little lamb";
    matrix[0] = ReversedSentence.reverseByCharacter(sentence).toCharArray();
    matrix[1] = ReversedSentence.reverseByWord(sentence).toCharArray();
    matrix[2] = ReversedSentence.change5thPosition(sentence).toCharArray();

    String expected =
        String.join(
            System.lineSeparator(),
            new String(matrix[0]),
            new String(matrix[1]),
            new String(matrix[2]));
    assertEquals(expected, ReversedSentence.printChar2DArray(matrix));
  }

  public void testTruncateSentence() {
    String sentence = "mary had a little lamb";
    assertEquals(sentence, ReversedSentence.truncateSentence(sentence));

    String result = ReversedSentence.truncateSentence(createString(90, 'x'));
    assertEquals(80, result.length());
    assertEquals(createString(80, 'x'), result);
  }

  private String createString(int length, char character) {
    StringBuffer buffer = new StringBuffer(length);
    for (int i = 0; i < length; i++) buffer.append(character);
    return buffer.toString();
  }
}