refactor
This commit is contained in:
coderfengyun 2014-07-03 09:04:02 +08:00
parent e26592c7a5
commit f304ff5a63
15 changed files with 72 additions and 235 deletions

View File

@ -1,113 +0,0 @@
package org.bench4q.agent.test;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.junit.Test;
public class LRUCache {
private int capacity;
private ListNode vHead;
private int last;
private Map<Integer, ListNode> keyNodes;
public LRUCache(int capacity) {
vHead = new ListNode(-1, -1, null, null);
this.keyNodes = new HashMap<Integer, ListNode>(capacity);
this.capacity = capacity;
}
public int get(int key) {
if (this.keyNodes.containsKey(key)) {
ListNode node = this.keyNodes.get(key);
delete(node);
offerFirst(node);
return node.val;
}
return -1;
}
private void delete(ListNode node) {
if (node.key == this.last) {
if (this.keyNodes.size() <= 1) {
this.last = Integer.MAX_VALUE;
} else {
this.last = node.previous.key;
}
}
node.previous.next = node.next;
if (node.next != null) {
node.next.previous = node.previous;
}
node.previous = null;
node.next = null;
this.keyNodes.remove(node.key);
}
private void offerFirst(ListNode node) {
node.next = vHead.next;
if (vHead.next != null) {
vHead.next.previous = node;
}
node.previous = vHead;
vHead.next = node;
if (this.keyNodes.size() == 0) {
this.last = node.key;
}
this.keyNodes.put(node.key, node);
}
public void set(int key, int value) {
ListNode valNode = null;
if (this.keyNodes.containsKey(key)) {
valNode = this.keyNodes.get(key);
valNode.val = value;
delete(valNode);
} else {
if (isFull()) {
delete(this.keyNodes.get(this.last));
}
valNode = new ListNode(key, value, null, null);
}
offerFirst(valNode);
}
private boolean isFull() {
return this.keyNodes.size() == this.capacity;
}
private class ListNode {
int key;
int val;
ListNode previous;
ListNode next;
public ListNode(int key, int val, ListNode previous, ListNode next) {
this.key = key;
this.val = val;
this.previous = previous;
this.next = next;
}
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.set(1, 1);
cache.set(2, 2);
cache.set(3, 3);
cache.set(4, 4);
System.out.println(cache.get(4));
System.out.println(cache.get(3));
System.out.println(cache.get(2));
System.out.println(cache.get(1));
cache.set(5, 5);
System.out.println(cache.get(1));
System.out.println(cache.get(2));
System.out.println(cache.get(3));
System.out.println(cache.get(4));
System.out.println(cache.get(5));
}
}

View File

@ -1,110 +1,64 @@
package org.bench4q.agent.test;
import static org.junit.Assert.*;
import java.util.LinkedList;
import org.junit.Test;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
private static boolean FIRST = true;
private static boolean SECOND = false;
public void reorderList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return;
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0) {
return 0;
}
ListNode startOfSecondHalf = findStartNodeOfSecondHalf(head);
startOfSecondHalf = reverse(startOfSecondHalf);
merge(head, startOfSecondHalf);
int result = ratings.length, minIndex = findMinIndex(ratings);
// Left part
for (int i = minIndex - 1; i >= 0; i--) {
}
// Right Part
for (int i = minIndex + 1; i <= ratings.length; i++) {
}
return result;
}
private ListNode findStartNodeOfSecondHalf(ListNode head) {
ListNode vHead = new ListNode(-1);
vHead.next = head;
ListNode runner = head, walker = head, walkerPre = vHead;
while (runner != null && runner.next != null) {
runner = runner.next;
runner = runner.next;
walkerPre = walkerPre.next;
walker = walker.next;
}
walkerPre.next = null;
return walker;
}
private ListNode reverse(ListNode start) {
ListNode vHead = new ListNode(-1), current = start, next = start.next;
while (current != null) {
current.next = vHead.next;
vHead.next = current;
current = next;
if (next == null) {
break;
private int findMinIndex(int[] target) {
int i = 0, maxValue = Integer.MIN_VALUE, result = -1;
while (i < target.length) {
if (target[i] > maxValue) {
maxValue = target[i];
result = i;
}
next = next.next;
}
return vHead.next;
return result;
}
private void merge(ListNode head, ListNode head2) {
ListNode current = head, firstCurrent = head.next, secondCurrent = head2, firstNext = firstCurrent != null ? firstCurrent.next
: null, secondNext = head2.next;
boolean currentTurn = SECOND;
while (firstCurrent != null && secondCurrent != null) {
if (currentTurn == FIRST) {
firstCurrent.next = null;
current.next = firstCurrent;
firstCurrent = firstNext;
if (firstNext != null) {
firstNext = firstNext.next;
}
currentTurn = SECOND;
} else {
secondCurrent.next = null;
current.next = secondCurrent;
secondCurrent = secondNext;
if (secondNext != null) {
secondNext = secondNext.next;
}
currentTurn = FIRST;
}
current = current.next;
}
current.next = firstCurrent == null ? secondCurrent : firstCurrent;
private int sumOfZeroTo(int target) {
return target * (target + 1) / 2;
}
@Test
public void test() {
ListNode list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list.next.next.next = new ListNode(4);
list.next.next.next.next = new ListNode(5);
reorderList(list);
print(list);
}
private void print(ListNode list) {
while (list != null) {
System.out.println(list.val);
list = list.next;
}
public void testAscending() {
assertEquals(15, this.candy(new int[] { 1, 2, 3, 4, 5 }));
}
@Test
public void test1() {
ListNode list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
reorderList(list);
print(list);
public void testAllEquals() {
assertEquals(5, this.candy(new int[] { 2, 2, 2, 2, 2 }));
}
@Test
public void testAscendingAndEquals() {
assertEquals(18, this.candy(new int[] { 1, 2, 3, 4, 4, 4 }));
}
@Test
public void testEqualsAndAscending() {
assertEquals(12, this.candy(new int[] { 1, 1, 1, 3, 4, 5 }));
}
@Test
public void testAscendingAndDescending() {
assertEquals(16, this.candy(new int[] { 1, 2, 3, 4, 3, 2, 1 }));
}
}

View File

@ -87,20 +87,10 @@ public class Agent {
agentWithoutInitial.getMaxLoad());
} else {
return createAgentWithoutId(agentWithoutInitial.getHostName(),
agentWithoutInitial.getPort());
agentWithoutInitial.getPort(), 500);
}
}
public static Agent createAgentWithoutId(String hostName, int port) {
Agent agent = new Agent();
agent.setCurrentEnumStatus(AgentStatus.InIdle);
agent.setHostName(hostName);
agent.setMaxLoad(500);
agent.setPort(port);
agent.setRemainLoad(500);
return agent;
}
public static Agent createAgentWithoutId(String hostName, int port,
int maxLoad) {
Agent agent = new Agent();

View File

@ -63,7 +63,8 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
FileUtils.readFileToString(scenarioFile));
inputModel.setPoolSize(20);
Agent agent = Agent.createAgentWithoutId(Test_HOSTNAME, Test_PORT);
Agent agent = Agent
.createAgentWithoutId(Test_HOSTNAME, Test_PORT, 1000);
RunScenarioResultModel model = this.getAgentMessenger().bookTest(agent,
20);
assertNotNull(model);

View File

@ -12,7 +12,7 @@ import org.bench4q.share.models.master.AgentModel;
import org.junit.Test;
public class Test_Agent {
private Agent agent = Agent.createAgentWithoutId("127.0.0.1", 6565);
private Agent agent = Agent.createAgentWithoutId("127.0.0.1", 6565, 1000);
public Agent getAgentForTest() {
return agent;

View File

@ -29,8 +29,8 @@ public class Test_RunningAgent extends TestBase_MakeUpTestPlan {
private List<Agent> testcase2 = new ArrayList<Agent>();
{
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565, 500));
}
@Before

View File

@ -22,8 +22,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_RunningScript extends TestBase_MakeUpTestPlan {
private List<Agent> testcase2 = new ArrayList<Agent>();
{
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565, 500));
}
@Before

View File

@ -35,7 +35,7 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan {
{
Random random = new Random(212);
testcase2.add(Agent.createAgentWithoutId(
random.nextInt(256) + ".0.0.2", 6565));
random.nextInt(256) + ".0.0.2", 6565, 500));
}
private TestPlanService testPlanService;
private TestPlanFactory testPlanFactory;

View File

@ -27,11 +27,11 @@ public class Test_highAvailableWithMockMessenger extends
{
testCase1.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
Agent agent = Agent.createAgentWithoutId("127.0.0.2", 6565);
Agent agent = Agent.createAgentWithoutId("127.0.0.2", 6565, 500);
agent.setCurrentEnumStatus(AgentStatus.BreakDown);
testCase1.add(agent);
Agent agent2 = Agent.createAgentWithoutId("127.0.0.3", 6565);
Agent agent2 = Agent.createAgentWithoutId("127.0.0.3", 6565, 500);
agent2.setCurrentEnumStatus(AgentStatus.InRunning);
agent2.bookTest(10);
testCase1.add(agent2);
@ -39,8 +39,8 @@ public class Test_highAvailableWithMockMessenger extends
private List<Agent> testcase2 = new ArrayList<Agent>();
{
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565, 500));
}
@Autowired

View File

@ -31,12 +31,12 @@ public class Test_AgentRepository {
String hostNameForTest = "133.133.12.95";
int portForTest = 6565;
assertTrue(this.getAgentRepoitory().attach(
Agent.createAgentWithoutId(hostNameForTest, portForTest)));
Agent.createAgentWithoutId(hostNameForTest, portForTest, 1000)));
Agent agent = this.getAgentRepoitory().getAgentBy(hostNameForTest);
assertNotNull(agent);
assertEquals(hostNameForTest, agent.getHostName());
assertEquals(portForTest, agent.getPort());
assertEquals(500, agent.getMaxLoad());
assertEquals(1000, agent.getMaxLoad());
assertEquals(AgentStatus.InIdle, agent.getCurrentEnumStatus());
System.out.println(AgentStatus.InIdle.ordinal());
assertTrue(this.getAgentRepoitory().detach(agent.getId()));

View File

@ -63,7 +63,7 @@ public class Test_AgentService {
private boolean doAddAgent() {
return this.getAgentService().addAgentToPool(
Agent.createAgentWithoutId(hostNameForTest, 6565));
Agent.createAgentWithoutId(hostNameForTest, 6565, 500));
}
@Before

View File

@ -76,7 +76,7 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult()
throws JAXBException {
this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"127.0.0.1", 6565));
"127.0.0.1", 6565, 500));
prepareForTestPlanRunning();
}
@ -89,8 +89,10 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
@Test
public void testLoadMemoryResults() throws InterruptedException,
Bench4QException {
assertNotNull(this.getAgentMessenger().getStatus(
Agent.createAgentWithoutId(Test_AGENT_HOSTNAME, TEST_PORT)));
assertNotNull(this.getAgentMessenger()
.getStatus(
Agent.createAgentWithoutId(Test_AGENT_HOSTNAME,
TEST_PORT, 500)));
User user = this.getUserRepository().getUser("admin");
int scriptId = getUserFirstScript(user);
this.getHaPool().checkAllHeartBeat();

View File

@ -34,7 +34,7 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
synchronized (Test_TestPlanEngine.class) {
this.getAgentRepository().detach("147.0.0.1");
this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"147.0.0.1", 6565));
"147.0.0.1", 6565, 500));
prepareForTestPlanRunning();
submitATestPlanWithOneScript();
}
@ -59,8 +59,10 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
@Test
public void testRunTestPlanRightly() throws InterruptedException {
assertNotNull(this.getAgentMessenger().getStatus(
Agent.createAgentWithoutId(Test_AGENT_HOSTNAME, TEST_PORT)));
assertNotNull(this.getAgentMessenger()
.getStatus(
Agent.createAgentWithoutId(Test_AGENT_HOSTNAME,
TEST_PORT, 500)));
User user = this.getUserRepository().getUser("admin");
int scriptId = getUserFirstScript(user);
this.getHaPool().checkAllHeartBeat();

View File

@ -39,7 +39,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
@Before
public void prepare() {
this.getAgentService().addAgentToPool(
Agent.createAgentWithoutId("127.0.0.1", 6565));
Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
}
@After

View File

@ -19,7 +19,8 @@ public class HighAvailableImpl implements HighAvailablePool {
public HighAvailableImpl() {
String hostName = "127.0.0.1";
this.pool.put(hostName, Agent.createAgentWithoutId(hostName, 6565));
this.pool
.put(hostName, Agent.createAgentWithoutId(hostName, 6565, 500));
}
@Override