Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete the test class for addFive TRYING again #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/main/java/tudelft/caesarshift/CaesarShiftCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

public class CaesarShiftCipher {

public String CaesarShiftCipher(String message, int shift){
public String caesarShiftCipher(String message, int shift){
StringBuilder sb = new StringBuilder();
char currentChar;
int length = message.length();

shift = shift%26;
shift = shift % 26;

for(int i = 0; i < length; i++){
for (int i = 0; i < length; i++){
currentChar = message.charAt(i);

sb.append(currentChar);
if (currentChar > 'z' || currentChar < 'a') {
// sb.append(currentChar);
//The above line is wrong as it tries to
//appending the original character and the shifted character to the StringBuilder
// within the loop. This results in each character being appended twice.
// You should append only the shifted character after the necessary checks.

if (currentChar < 'a' || currentChar > 'z') {
// Handle invalid characters here, either throw an exception or return a special value
return "invalid";
} else if ((char) (currentChar + shift) > 'z') {
currentChar = (char) (currentChar - 26);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/tudelft/ghappy/GHappy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ public boolean gHappy(String str) {
assert str!=null;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) == 'g') {
if (i >= 0 && str.charAt(i-1) == 'g') { continue; }
if (i > 0 && str.charAt(i-1) == 'g') { continue; }
if (i+1 < str.length() && str.charAt(i+1) == 'g') { continue; }
return false;
}
}

return true;

}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/tudelft/numfinder/NumFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ public void find(int[] nums) {

if(n < smallest)
smallest = n;
else if (n > largest)
if (n > largest)
largest = n;

}
}

public int getSmallest () {
return smallest;
public int getSmallest () {return smallest;
}

public int getLargest () {
return largest;
public int getLargest () {return largest;
}
}
2 changes: 1 addition & 1 deletion src/main/java/tudelft/numfinder/NumFinderMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main (String[] args) {
// nf.find(new int[] {4, 25, 7, 9});

// this crashes
nf.find(new int[] {4, 3, 2, 1});
nf.find(new int[] {4, 3, 2, 1, 0,});

System.out.println(nf.getLargest());
System.out.println(nf.getSmallest());
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package tudelft.caesarshift;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tudelft.ghappy.GHappy;

/**
* A Caesar Cipher is a simply way to encrypt messages.
* There are two inputs: A message and a shift.
* The message can be any string containing lower case letters and spaces,
* any other characters will return 'invalid' The shift can be any positive or negative integer.
* Each letter of the message is then shifted by the specified amount
* (if shift is '3' then 'abc' becomes 'def' and 'xyz' becomes 'abc').
*
* See our implementation in the CaesarCipherShift class (package tudelft.caesarcipher).
* There's a bug in this implementation. Apply everything you learned and do your best to find the bug!!
* Write your automated tests inside CaeserCipherShiftTest class.
* At the end, push your solution to your GitHub repository.
*/

public class CaesarShiftCipherTest {
private CaesarShiftCipher encrypt;
@BeforeEach
public void initialize() {this.encrypt = new CaesarShiftCipher();}

@Test
public void encryptTest_01() {
String encrypt_A = encrypt.caesarShiftCipher("abc",3);
Assertions.assertEquals("def", encrypt_A);
}
@Test
public void encryptTest_02() {
String encrypt_A = encrypt.caesarShiftCipher("xyz",3);
Assertions.assertEquals("abc", encrypt_A);
}
@Test
public void encryptTest_03() {
String encrypt_A = encrypt.caesarShiftCipher("ABC",3);
Assertions.assertEquals("invalid", encrypt_A);
//Capital letters should return invalid
}
@Test
public void encryptTest_04() {
String encrypt_A = encrypt.caesarShiftCipher("abc",4);
Assertions.assertEquals("efg", encrypt_A);
// shifting at 4 spaces
}
@Test
public void encryptTest_05() {
String encrypt_A = encrypt.caesarShiftCipher("ace",3);
Assertions.assertEquals("dfh", encrypt_A);
//a plus 3 shifts equals d, c plus 3 shifts is f and e plus 3 shifts is h, "dfh"
}
@Test
public void encryptTest_06() {
String encrypt_A = encrypt.caesarShiftCipher("123",3);
Assertions.assertEquals("invalid", encrypt_A);
// "456" is not expected as anything out small letters 'a to z' is "invalid"
}

}
26 changes: 14 additions & 12 deletions src/test/java/tudelft/gettingstarted/GettingStartedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
public class GettingStartedTest {

@Test
public void addFiveTo20() {
public void addFiveToTwenty() {
int result = new GettingStarted().addFive(20);
Assertions.assertEquals(25,result);
}

// UNCOMMENT THE CODE BELOW, AND FILL THE GAPS!

// @Test
// public void addFiveToZero() {
// int result = new GettingStarted().addFive(???);
// Assertions.assertEquals(???, result);
// }
//
// @Test
// public void addFiveToMinus20() {
// int result = new GettingStarted().addFive(???);
// Assertions.assertEquals(????,result);
// }
@Test
// filling this in with 0 and 5
public void addFiveToZero() {
int result = new GettingStarted().addFive(0);
Assertions.assertEquals(5, result);
}

@Test
// filling this in with -20 and -15
public void addFiveToMinus20() {
int result = new GettingStarted().addFive(-20);
Assertions.assertEquals(-15,result);
}
}
54 changes: 54 additions & 0 deletions src/test/java/tudelft/ghappy/GHappyTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
package tudelft.ghappy;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


public class GHappyTest {
private GHappy G;

@BeforeEach
public void initialize() {this.G = new GHappy();}

/**
* We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right.
* Return true if all the g's in the given string are happy.
*
* Examples:
* gHappy("xxggxx") ==> true
* gHappy("xxgxx") ==> false
* gHappy("xxggyygxx") ==> false
* See our implementation in the GHappy class (package tudelft.ghappy).
* There's a bug in this implementation. Apply everything you learned and do your best to find the bug!!
* Write your automated tests inside GHappyTest class.
* At the end, push your solution to your GitHub repository.
*
* Test the following conditions
* checkG_001 gHappy("xxggxx") ==> true
* checkG_002 gHappy("xxgxx") ==> false
* checkG_003 gHappy("xxggyygxx") ==> false
*/
@Test
public void checkG_001() {
boolean check001 = G.gHappy("ABggCD");
Assertions.assertTrue(check001);
}
@Test
public void checkG_002() {
boolean check001 = G.gHappy("ABgCD");
Assertions.assertFalse(check001);
}
@Test
public void checkG_003() {
boolean check001 = G.gHappy("ABggCDgEF");
Assertions.assertFalse(check001);
}
@Test
public void checkG_004() {
boolean check001 = G.gHappy("ggABggCD");
Assertions.assertTrue(check001);
}
@Test
public void checkG_005() {
boolean check001 = G.gHappy("ABggCDgg");
Assertions.assertTrue(check001);
}
}

14 changes: 13 additions & 1 deletion src/test/java/tudelft/roman/RomanNumeralTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tudelft.roman;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class RomanNumeralTest {


@Test
public void singleNumber() {
RomanNumeral roman = new RomanNumeral();
Expand Down Expand Up @@ -33,4 +33,16 @@ public void numberWithAndWithoutSubtractiveNotation() {
int result = roman.convert("XLIV");
Assertions.assertEquals(44, result);
}
@Test
public void numberWithandWithoutLargeSubtractiveNotation() {
RomanNumeral roman = new RomanNumeral();
int result = roman.convert("XCIX");
Assertions.assertEquals(99, result);
}
@Test
public void numberWithLargeSubtractiveNotation() {
RomanNumeral roman = new RomanNumeral();
int result = roman.convert("CD");
Assertions.assertEquals(400, result);
}
}
14 changes: 11 additions & 3 deletions src/test/java/tudelft/roman/RomanNumeralTestWithBeforeEach.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class RomanNumeralTestWithBeforeEach {
private RomanNumeral roman;

@BeforeEach
public void initialize() {
this.roman = new RomanNumeral();
}
public void initialize() {this.roman = new RomanNumeral();}

@Test
public void singleNumber() {
Expand All @@ -36,4 +34,14 @@ public void numberWithAndWithoutSubtractiveNotation() {
int result = roman.convert("XLIV");
Assertions.assertEquals(44, result);
}
@Test
public void numberWithandWithoutLargeSubtractiveNotation() {
int result = roman.convert("XCIX");
Assertions.assertEquals(99, result);
}
@Test
public void numberWithLargeSubtractiveNotation() {
int result = roman.convert("CD");
Assertions.assertEquals(400, result);
}
}