Skip to content

Commit 449961b

Browse files
authored
feat: test coverage implementation of Base64Test.java
1 parent bb6acd3 commit 449961b

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
/**
11+
* Test cases for Base64 encoding and decoding.
12+
*
13+
* Author: Nithin U.
14+
* Github: https://github.com/NithinU2802
15+
*/
16+
17+
class Base64Test {
18+
19+
@Test
20+
void testBase64Alphabet() {
21+
// Test that all Base64 characters are handled correctly
22+
String allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23+
String encoded = Base64.encode(allChars);
24+
String decoded = Base64.decodeToString(encoded);
25+
assertEquals(allChars, decoded);
26+
}
27+
28+
@ParameterizedTest
29+
@CsvSource({
30+
"'', ''",
31+
"A, QQ==",
32+
"AB, QUI=",
33+
"ABC, QUJD",
34+
"ABCD, QUJDRA==",
35+
"Hello, SGVsbG8=",
36+
"'Hello World', SGVsbG8gV29ybGQ=",
37+
"'Hello, World!', 'SGVsbG8sIFdvcmxkIQ=='",
38+
"'The quick brown fox jumps over the lazy dog', 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=='",
39+
"123456789, MTIzNDU2Nzg5",
40+
"'Base64 encoding test', 'QmFzZTY0IGVuY29kaW5nIHRlc3Q='"
41+
})
42+
void testStringEncoding(String input, String expected) {
43+
assertEquals(expected, Base64.encode(input));
44+
}
45+
46+
@ParameterizedTest
47+
@CsvSource({
48+
"'', ''",
49+
"QQ==, A",
50+
"QUI=, AB",
51+
"QUJD, ABC",
52+
"QUJDRA==, ABCD",
53+
"SGVsbG8=, Hello",
54+
"'SGVsbG8gV29ybGQ=', 'Hello World'",
55+
"'SGVsbG8sIFdvcmxkIQ==', 'Hello, World!'",
56+
"'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==', 'The quick brown fox jumps over the lazy dog'",
57+
"MTIzNDU2Nzg5, 123456789",
58+
"'QmFzZTY0IGVuY29kaW5nIHRlc3Q=', 'Base64 encoding test'"
59+
})
60+
void testStringDecoding(String input, String expected) {
61+
assertEquals(expected, Base64.decodeToString(input));
62+
}
63+
64+
@Test
65+
void testByteArrayEncoding() {
66+
byte[] input = {72, 101, 108, 108, 111};
67+
String expected = "SGVsbG8=";
68+
assertEquals(expected, Base64.encode(input));
69+
}
70+
71+
@Test
72+
void testByteArrayDecoding() {
73+
String input = "SGVsbG8=";
74+
byte[] expected = {72, 101, 108, 108, 111};
75+
assertArrayEquals(expected, Base64.decode(input));
76+
}
77+
78+
@Test
79+
void testRoundTripEncoding() {
80+
String[] testStrings = {
81+
"",
82+
"A",
83+
"AB",
84+
"ABC",
85+
"Hello, World!",
86+
"The quick brown fox jumps over the lazy dog",
87+
"1234567890",
88+
"Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?",
89+
"Unicode: வணக்கம்", // Tamil for "Hello"
90+
"Multi-line\nstring\rwith\tdifferent\nwhitespace"
91+
};
92+
93+
for (String original : testStrings) {
94+
String encoded = Base64.encode(original);
95+
String decoded = Base64.decodeToString(encoded);
96+
assertEquals(original, decoded, "Round trip failed for: " + original);
97+
}
98+
}
99+
100+
@Test
101+
void testRoundTripByteArrayEncoding() {
102+
byte[][] testArrays = {
103+
{},
104+
{0},
105+
{-1},
106+
{0, 1, 2, 3, 4, 5},
107+
{-128, -1, 0, 1, 127},
108+
{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
109+
};
110+
111+
for (byte[] original : testArrays) {
112+
String encoded = Base64.encode(original);
113+
byte[] decoded = Base64.decode(encoded);
114+
assertArrayEquals(original, decoded, "Round trip failed for byte array");
115+
}
116+
}
117+
118+
@Test
119+
void testBinaryData() {
120+
// Test with binary data that might contain null bytes
121+
byte[] binaryData = new byte[256];
122+
for (int i = 0; i < 256; i++) {
123+
binaryData[i] = (byte) i;
124+
}
125+
126+
String encoded = Base64.encode(binaryData);
127+
byte[] decoded = Base64.decode(encoded);
128+
assertArrayEquals(binaryData, decoded);
129+
}
130+
131+
@Test
132+
void testNullInputEncoding() {
133+
assertThrows(IllegalArgumentException.class, () -> Base64.encode((String) null));
134+
assertThrows(IllegalArgumentException.class, () -> Base64.encode((byte[]) null));
135+
}
136+
137+
@Test
138+
void testNullInputDecoding() {
139+
assertThrows(IllegalArgumentException.class, () -> Base64.decode(null));
140+
assertThrows(IllegalArgumentException.class, () -> Base64.decodeToString(null));
141+
}
142+
143+
@Test
144+
void testInvalidBase64Characters() {
145+
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8@"));
146+
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8#"));
147+
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8$"));
148+
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8%"));
149+
}
150+
151+
@Test
152+
void testPaddingVariations() {
153+
// Test different padding scenarios '='
154+
assertEquals("A", Base64.decodeToString("QQ=="));
155+
assertEquals("AB", Base64.decodeToString("QUI="));
156+
assertEquals("ABC", Base64.decodeToString("QUJD"));
157+
}
158+
159+
@Test
160+
void testPaddingConsistency() {
161+
// Ensure that strings requiring different amounts of padding encode/decode correctly
162+
String[] testCases = {"A", "AB", "ABC", "ABCD", "ABCDE", "ABCDEF"};
163+
164+
for (String test : testCases) {
165+
String encoded = Base64.encode(test);
166+
String decoded = Base64.decodeToString(encoded);
167+
assertEquals(test, decoded);
168+
169+
// Verify padding is correct
170+
int expectedPadding = (3 - (test.length() % 3)) % 3;
171+
int actualPadding = 0;
172+
for (int i = encoded.length() - 1; i >= 0 && encoded.charAt(i) == '='; i--) {
173+
actualPadding++;
174+
}
175+
assertEquals(expectedPadding, actualPadding, "Incorrect padding for: " + test);
176+
}
177+
}
178+
179+
@Test
180+
void testLargeData() {
181+
// Test with larger data to ensure scalability
182+
StringBuilder largeString = new StringBuilder();
183+
for (int i = 0; i < 1000; i++) {
184+
largeString.append("This is a test string for Base64 encoding. ");
185+
}
186+
187+
String original = largeString.toString();
188+
String encoded = Base64.encode(original);
189+
String decoded = Base64.decodeToString(encoded);
190+
assertEquals(original, decoded);
191+
}
192+
193+
@Test
194+
void testEmptyAndSingleCharacter() {
195+
// Test edge cases
196+
assertEquals("", Base64.encode(""));
197+
assertEquals("", Base64.decodeToString(""));
198+
199+
assertEquals("QQ==", Base64.encode("A"));
200+
assertEquals("A", Base64.decodeToString("QQ=="));
201+
}
202+
203+
}

0 commit comments

Comments
 (0)