Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.
Merged
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
5 changes: 4 additions & 1 deletion src/main/java/MakeItFit/utils/EmailValidator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package MakeItFit.utils;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
Expand All @@ -11,7 +10,11 @@
*/
public class EmailValidator {

/* fails several tests
private static final String EMAIL_PATTERN = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
*/
private static final String EMAIL_PATTERN =
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

/**
* Verifies if the email is valid.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/MakeItFit/utils/MyTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
* @author Afonso Santos (a104276), Hélder Gomes (a104100) and Pedro Pereira (a104082)
* @version (11052024)
*/
/* References to generic type MyTuple<T1,T2> should be parameterized
public class MyTuple<T1, T2> implements Serializable, Comparable<MyTuple> {
*/
public class MyTuple<T1, T2> implements Serializable, Comparable<MyTuple<T1, T2>> {

private final T1 item1;
private final T2 item2;
Expand Down Expand Up @@ -81,7 +84,10 @@ public boolean equals(Object o) {
* or greater than the other tuple
*/
@Override
/* References to generic type MyTuple<T1,T2> should be parameterized
public int compareTo(MyTuple other) {
*/
public int compareTo(MyTuple<T1, T2> other) {
int compareItem1 = this.item1.toString().compareTo(other.item1.toString());
if (compareItem1 == 0) {
return this.item2.toString().compareTo(other.item2.toString());
Expand All @@ -95,7 +101,11 @@ public int compareTo(MyTuple other) {
* @return A new MyTuple instance that is a copy of the current instance.
*/
@Override
/* References to generic type MyTuple<T1,T2> should be parameterized
public MyTuple clone() {
return new MyTuple(this.item1, this.item2);
*/
public MyTuple<T1, T2> clone() {
return new MyTuple<T1, T2>(this.item1, this.item2);
}
}
93 changes: 93 additions & 0 deletions src/unittests/java/MakeItFit/utils/EmailValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package MakeItFit.utils;

import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class EmailValidatorTest {
@Test
void testQuotedAndDoubleDot() {
assertTrue(EmailValidator.isValidEmail("\"john..doe\"@uminho.pt"));
}

@Test
void testIPv4LiteralDomain() {
assertTrue(EmailValidator.isValidEmail("user@[192.168.0.1]"));
}

@Test
void testLocalBangAndPercent() {
assertTrue(EmailValidator.isValidEmail("user%example.com@example.org"));
}

@Test
void testSingleLetters() {
assertTrue(EmailValidator.isValidEmail("x@y.co"));
}

@Test
void testLocalQuotedSingleLetter() {
assertTrue(EmailValidator.isValidEmail("\"a\"@b.co"));
}

@Test
void testLocalPlusTag() {
assertTrue(EmailValidator.isValidEmail("first.last+tag@example.com"));
}

@Test
void testLocalAndDomainHyphenOrUnderscore() {
assertTrue(EmailValidator.isValidEmail("first_last-123@sub-domain.example.co.uk"));
}

@Test
void testDomainStartsWithHyphen() {
assertFalse(EmailValidator.isValidEmail("user@-example.com"));
}

@Test
void testDomainEndsWithHyphen() {
assertFalse(EmailValidator.isValidEmail("user@example-.com"));
}

@Test
void testDomainConsecutiveDots() {
assertFalse(EmailValidator.isValidEmail("user@sub..example.com"));
}

@Test
void testLocalTrailingDot() {
assertFalse(EmailValidator.isValidEmail("user.@example.com"));
}

@Test
void testLocalStartsWithDot() {
assertFalse(EmailValidator.isValidEmail(".user@example.com"));
}

@Test
void testDomainSpace() {
assertFalse(EmailValidator.isValidEmail("user@exa mple.com"));
}

@Test
void testTLDMissing() {
assertFalse(EmailValidator.isValidEmail("user@example"));
}

@Test
void testQuotedAndSpace() {
assertFalse(EmailValidator.isValidEmail("\"john doe\"@uminho.pt"));
}

@Test
void testEmpty() {
assertFalse(EmailValidator.isValidEmail(""));
}

@Test
void testToForceCoverage() {
new EmailValidator();
}
}
65 changes: 65 additions & 0 deletions src/unittests/java/MakeItFit/utils/ExtendedRandomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package MakeItFit.utils;

import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class ExtendedRandomTest {
private int SAMPLE_SIZE = 1000;

@Test
void testRangeBoundariesUnseededRandom() {
ExtendedRandom unseededRandom = new ExtendedRandom();
for (int i = 0; i < SAMPLE_SIZE; i++) {
int expected = unseededRandom.nextInt(5, 15);
assertTrue(expected >= 5);
assertTrue(expected < 15);
}
}

@Test
void testDeterministicOutputSeededRandom() {
ExtendedRandom seededRandom = new ExtendedRandom(123456L);
int[] firstRun = new int[5];
for (int i = 0; i < firstRun.length; i++) {
firstRun[i] = seededRandom.nextInt(5, 10);
}

ExtendedRandom repeatRandom = new ExtendedRandom(123456L);
for (int i = 0; i < firstRun.length; i++) {
assertEquals(firstRun[i], repeatRandom.nextInt(5, 10));
}
}

@Test
void testOriginEqualThrows() {
ExtendedRandom unseededRandom = new ExtendedRandom();
assertThrows(IllegalArgumentException.class, () -> unseededRandom.nextInt(10, 10));
}

@Test
void testOriginGreaterThrows() {
ExtendedRandom unseededRandom = new ExtendedRandom();
assertThrows(IllegalArgumentException.class, () -> unseededRandom.nextInt(15, 5));
}

@Test
void testSingleValueRange() {
ExtendedRandom unseededRandom = new ExtendedRandom();
for (int i = 0; i < SAMPLE_SIZE; i++) {
assertEquals(5, unseededRandom.nextInt(5, 6));
}
}

@Test
void testNegativeOriginPositiveBound() {
ExtendedRandom unseededRandom = new ExtendedRandom();
for (int i = 0; i < SAMPLE_SIZE; i++) {
int expected = unseededRandom.nextInt(-5, 5);
assertTrue(expected >= -5);
assertTrue(expected < 5);
}
}
}
Loading