forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Lizhny/master
补交第三次作业,多线程下载
- Loading branch information
Showing
15 changed files
with
772 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
group26/lizhy2017/homework/second/array/ArrayUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package second.array; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* ${} | ||
* Created by spark_lizhy on 2017/3/20. | ||
*/ | ||
|
||
public class ArrayUtilTest { | ||
|
||
private int[] temp; | ||
private int size; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
size = 10; | ||
temp = new int[size]; | ||
for (int i = 0; i < size; i++) { | ||
temp[i] = i; | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void reverseArray() throws Exception { | ||
ArrayUtil.reverseArray(temp); | ||
for (int i = 0; i < size; i++) { | ||
Assert.assertEquals(size - 1 - i, temp[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void removeZero() throws Exception { | ||
for (int i = 0; i < size; i++) { | ||
if (i % 5 == 0) { | ||
temp[i] = 0; | ||
} else { | ||
temp[i] = i; | ||
} | ||
} | ||
|
||
temp = ArrayUtil.removeZero(temp); | ||
Assert.assertNotNull(temp); | ||
for (int i = 0; i < temp.length; i++) { | ||
if (temp[i] != 0) { | ||
continue; | ||
} | ||
Assert.assertEquals(temp[i], i); | ||
} | ||
|
||
// 测试空数组 | ||
{ | ||
int[] testArray = new int[5]; | ||
for (int i = 0; i < 5; i++) { | ||
testArray[i] = 0; | ||
} | ||
|
||
int[] newArray = ArrayUtil.removeZero(testArray); | ||
Assert.assertNotNull(newArray); | ||
Assert.assertEquals(newArray.length, 0); | ||
} | ||
} | ||
|
||
@Test | ||
public void merge() throws Exception { | ||
// 构建数组 | ||
int[] array1 = new int[10]; | ||
int[] array2 = new int[11]; | ||
array2[10] = 100; | ||
for (int i = 0; i < 10; i++) { | ||
if (i % 2 == 0) { | ||
array1[i / 2] = i; // 0, 2, 4, 6, 8 | ||
} else { | ||
array2[i / 2] = i; // 1, 3, 5, 7, 9 | ||
} | ||
} | ||
|
||
// 测试merge | ||
{ | ||
int[] merge = ArrayUtil.merge(array1, array2); | ||
Assert.assertNotNull(merge); | ||
Assert.assertEquals(merge.length, 21); | ||
} | ||
|
||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group26/lizhy2017/homework/third/basic/LRUPageFameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package third.basic; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* ${} | ||
* Created by spark_lizhy on 2017/3/31. | ||
*/ | ||
|
||
public class LRUPageFameTest { | ||
@Test | ||
public void testAccess() { | ||
LRUPageFrame frame = new LRUPageFrame(3); | ||
frame.access(7); | ||
frame.access(0); | ||
frame.access(1); | ||
Assert.assertEquals("1,0,7", frame.toString()); | ||
frame.access(2); | ||
Assert.assertEquals("2,1,0", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(3); | ||
Assert.assertEquals("3,0,2", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,3,2", frame.toString()); | ||
frame.access(4); | ||
Assert.assertEquals("4,0,3", frame.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package third.basic; | ||
|
||
/** | ||
* ${} | ||
* Created by spark_lizhy on 2017/3/31. | ||
*/ | ||
/** | ||
* 用双向链表实现 LRU 算法 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class LRUPageFrame { | ||
|
||
private static class Node { | ||
|
||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LRUPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
|
||
|
||
} | ||
|
||
|
||
|
||
public String toString(){ | ||
StringBuilder buffer = new StringBuilder(); | ||
Node node = first; | ||
while(node != null){ | ||
buffer.append(node.pageNum); | ||
|
||
node = node.next; | ||
if(node != null){ | ||
buffer.append(","); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.