Skip to content

Commit e2de644

Browse files
committed
157 (2) add tests
1 parent ac97e00 commit e2de644

File tree

5 files changed

+242
-41
lines changed

5 files changed

+242
-41
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
***************************************************************************
3+
* Description:
4+
*
5+
* The API: int read4(char *buf) reads 4 characters at a time from a file.
6+
*
7+
* The return value is the actual number of characters read. For example,
8+
* it returns 3 if there is only 3 characters left in the file.
9+
*
10+
* By using the read4 API, implement the function int read(char *buf, int n)
11+
* that reads n characters from the file.
12+
*
13+
* Note: The read function will only be called once for each test case.
14+
*
15+
***************************************************************************
16+
* @tag : String
17+
* {@link https://leetcode.com/problems/read-n-characters-given-read4/ }
18+
*/
19+
package _157_ReadNCharactersGivenRead4;
20+
21+
/** see test {@link _157_ReadNCharactersGivenRead4.PracticeTest } */
22+
/*
23+
* The read4 API is defined in the parent class Reader4. int read4(char[] buf);
24+
*/
25+
public class Practice extends Reader4 {
26+
27+
/**
28+
* @param buf Destination buffer
29+
* @param n Maximum number of characters to read
30+
* @return The number of characters read
31+
*/
32+
public int read(char[] buf, int n) {
33+
// TODO:
34+
return 0;
35+
}
36+
37+
}

src/_157_ReadNCharactersGivenRead4/Reader4.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@
22

33
public class Reader4 {
44

5+
private char[] _contents;
6+
7+
private int _offset;
8+
9+
private static final int READ_SIZE = 4;
10+
11+
public Reader4() {
12+
_contents = null;
13+
_offset = 0;
14+
}
15+
16+
public void setContents(char[] contents) {
17+
this._contents = contents;
18+
}
19+
520
public int read4(char[] buf) {
6-
return 0;
21+
int sz = Math.min(_contents.length - _offset, READ_SIZE);
22+
for (int i = 0; i < sz; i++) {
23+
buf[i] = _contents[_offset + i];
24+
}
25+
_offset += sz;
26+
return sz;
727
}
828

929
}

src/_157_ReadNCharactersGivenRead4/SolutionTest.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package _157_ReadNCharactersGivenRead4;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class PracticeTest {
12+
13+
/** Test method for {@link _157_ReadNCharactersGivenRead4.Practice } */
14+
Practice reader;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
reader = new Practice();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
reader = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
char[] contents = { 'a' };
32+
reader.setContents(contents);
33+
34+
int n = 1;
35+
char[] buf = new char[n];
36+
int actual = reader.read(buf, n);
37+
int expected = 1;
38+
assertEquals(expected, actual);
39+
char[] exps = { 'a' };
40+
assertArrayEquals(exps, buf);
41+
}
42+
43+
@Test
44+
public void Test2() {
45+
char[] contents = { 'a' , 'd', 'c', 'b' };
46+
reader.setContents(contents);
47+
48+
int n = 2;
49+
char[] buf = new char[n];
50+
int actual = reader.read(buf, n);
51+
int expected = 2;
52+
assertEquals(expected, actual);
53+
char[] exps = { 'a', 'd' };
54+
assertArrayEquals(exps, buf);
55+
}
56+
57+
@Test
58+
public void Test3() {
59+
char[] contents = { 'x' , 'y' };
60+
reader.setContents(contents);
61+
62+
int n = 5;
63+
char[] buf = new char[n];
64+
int actual = reader.read(buf, n);
65+
int expected = 2;
66+
assertEquals(expected, actual);
67+
char[] exps = new char[n];
68+
exps[0] = 'x';
69+
exps[1] = 'y';
70+
assertArrayEquals(exps, buf);
71+
}
72+
73+
@Test
74+
public void Test4() {
75+
char[] contents = { 'a', 'b', 'c', 'd', 'e'};
76+
reader.setContents(contents);
77+
78+
int n = 7;
79+
char[] buf = new char[n];
80+
int actual = reader.read(buf, n);
81+
int expected = 5;
82+
assertEquals(expected, actual);
83+
char[] exps = new char[n];
84+
exps[0] = 'a';
85+
exps[1] = 'b';
86+
exps[2] = 'c';
87+
exps[3] = 'd';
88+
exps[4] = 'e';
89+
assertArrayEquals(exps, buf);
90+
}
91+
92+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package _157_ReadNCharactersGivenRead4;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class SolutionTest {
12+
13+
/** Test method for {@link _157_ReadNCharactersGivenRead4.Solution } */
14+
Solution reader;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
reader = new Solution();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
reader = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
char[] contents = { 'a' };
32+
reader.setContents(contents);
33+
34+
int n = 1;
35+
char[] buf = new char[n];
36+
int actual = reader.read(buf, n);
37+
int expected = 1;
38+
assertEquals(expected, actual);
39+
char[] exps = { 'a' };
40+
assertArrayEquals(exps, buf);
41+
}
42+
43+
@Test
44+
public void Test2() {
45+
char[] contents = { 'a' , 'd', 'c', 'b' };
46+
reader.setContents(contents);
47+
48+
int n = 2;
49+
char[] buf = new char[n];
50+
int actual = reader.read(buf, n);
51+
int expected = 2;
52+
assertEquals(expected, actual);
53+
char[] exps = { 'a', 'd' };
54+
assertArrayEquals(exps, buf);
55+
}
56+
57+
@Test
58+
public void Test3() {
59+
char[] contents = { 'x' , 'y' };
60+
reader.setContents(contents);
61+
62+
int n = 5;
63+
char[] buf = new char[n];
64+
int actual = reader.read(buf, n);
65+
int expected = 2;
66+
assertEquals(expected, actual);
67+
char[] exps = new char[n];
68+
exps[0] = 'x';
69+
exps[1] = 'y';
70+
assertArrayEquals(exps, buf);
71+
}
72+
73+
@Test
74+
public void Test4() {
75+
char[] contents = { 'a', 'b', 'c', 'd', 'e'};
76+
reader.setContents(contents);
77+
78+
int n = 7;
79+
char[] buf = new char[n];
80+
int actual = reader.read(buf, n);
81+
int expected = 5;
82+
assertEquals(expected, actual);
83+
char[] exps = new char[n];
84+
exps[0] = 'a';
85+
exps[1] = 'b';
86+
exps[2] = 'c';
87+
exps[3] = 'd';
88+
exps[4] = 'e';
89+
assertArrayEquals(exps, buf);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy