Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2de644

Browse files
committed
157 (2) add tests
1 parentac97e00 commite2de644

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+
publicclassPracticeextendsReader4 {
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+
publicintread(char[]buf,intn) {
33+
// TODO:
34+
return0;
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
publicclassReader4 {
44

5+
privatechar[]_contents;
6+
7+
privateint_offset;
8+
9+
privatestaticfinalintREAD_SIZE =4;
10+
11+
publicReader4() {
12+
_contents =null;
13+
_offset =0;
14+
}
15+
16+
publicvoidsetContents(char[]contents) {
17+
this._contents =contents;
18+
}
19+
520
publicintread4(char[]buf) {
6-
return0;
21+
intsz =Math.min(_contents.length -_offset,READ_SIZE);
22+
for (inti =0;i <sz;i++) {
23+
buf[i] =_contents[_offset +i];
24+
}
25+
_offset +=sz;
26+
returnsz;
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+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassPracticeTest {
12+
13+
/** Test method for {@link _157_ReadNCharactersGivenRead4.Practice } */
14+
Practicereader;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
reader =newPractice();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
reader =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
char[]contents = {'a' };
32+
reader.setContents(contents);
33+
34+
intn =1;
35+
char[]buf =newchar[n];
36+
intactual =reader.read(buf,n);
37+
intexpected =1;
38+
assertEquals(expected,actual);
39+
char[]exps = {'a' };
40+
assertArrayEquals(exps,buf);
41+
}
42+
43+
@Test
44+
publicvoidTest2() {
45+
char[]contents = {'a' ,'d','c','b' };
46+
reader.setContents(contents);
47+
48+
intn =2;
49+
char[]buf =newchar[n];
50+
intactual =reader.read(buf,n);
51+
intexpected =2;
52+
assertEquals(expected,actual);
53+
char[]exps = {'a','d' };
54+
assertArrayEquals(exps,buf);
55+
}
56+
57+
@Test
58+
publicvoidTest3() {
59+
char[]contents = {'x' ,'y' };
60+
reader.setContents(contents);
61+
62+
intn =5;
63+
char[]buf =newchar[n];
64+
intactual =reader.read(buf,n);
65+
intexpected =2;
66+
assertEquals(expected,actual);
67+
char[]exps =newchar[n];
68+
exps[0] ='x';
69+
exps[1] ='y';
70+
assertArrayEquals(exps,buf);
71+
}
72+
73+
@Test
74+
publicvoidTest4() {
75+
char[]contents = {'a','b','c','d','e'};
76+
reader.setContents(contents);
77+
78+
intn =7;
79+
char[]buf =newchar[n];
80+
intactual =reader.read(buf,n);
81+
intexpected =5;
82+
assertEquals(expected,actual);
83+
char[]exps =newchar[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+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassSolutionTest {
12+
13+
/** Test method for {@link _157_ReadNCharactersGivenRead4.Solution } */
14+
Solutionreader;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
reader =newSolution();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
reader =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
char[]contents = {'a' };
32+
reader.setContents(contents);
33+
34+
intn =1;
35+
char[]buf =newchar[n];
36+
intactual =reader.read(buf,n);
37+
intexpected =1;
38+
assertEquals(expected,actual);
39+
char[]exps = {'a' };
40+
assertArrayEquals(exps,buf);
41+
}
42+
43+
@Test
44+
publicvoidTest2() {
45+
char[]contents = {'a' ,'d','c','b' };
46+
reader.setContents(contents);
47+
48+
intn =2;
49+
char[]buf =newchar[n];
50+
intactual =reader.read(buf,n);
51+
intexpected =2;
52+
assertEquals(expected,actual);
53+
char[]exps = {'a','d' };
54+
assertArrayEquals(exps,buf);
55+
}
56+
57+
@Test
58+
publicvoidTest3() {
59+
char[]contents = {'x' ,'y' };
60+
reader.setContents(contents);
61+
62+
intn =5;
63+
char[]buf =newchar[n];
64+
intactual =reader.read(buf,n);
65+
intexpected =2;
66+
assertEquals(expected,actual);
67+
char[]exps =newchar[n];
68+
exps[0] ='x';
69+
exps[1] ='y';
70+
assertArrayEquals(exps,buf);
71+
}
72+
73+
@Test
74+
publicvoidTest4() {
75+
char[]contents = {'a','b','c','d','e'};
76+
reader.setContents(contents);
77+
78+
intn =7;
79+
char[]buf =newchar[n];
80+
intactual =reader.read(buf,n);
81+
intexpected =5;
82+
assertEquals(expected,actual);
83+
char[]exps =newchar[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)

[8]ページ先頭

©2009-2025 Movatter.jp