view src/pathfinder/BlockingQnoSeMa/TestMerger.java @ 91:0a483aa8cf71

SJIS to UTF-8
author kent
date Fri, 21 Dec 2007 17:49:22 +0900
parents 3db21fae825a
children 6c209de0dd99
line wrap: on
line source

package pathfinder.BlockingQnoSeMa;

import java.util.LinkedList;
import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;

public class TestMerger {
	static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
	private int N_editor;
	private NetworkSimulator<REPCommand> ns;
	private LinkedList<EditorSimulator> editors;

	public TestMerger(int editor, int packet){
		N_editor = editor;
		ns = new NetworkSimulator<REPCommand>();
		editors = new LinkedList<EditorSimulator>();
	}

	public static void main(String[] args){
		TestMerger tm;
		tm = new TestMerger(4, 3);
		if(true){
			tm.test1cmd();
		}else{
			tm.test3cmd4editor();
			tm.test2cmd();
			tm.test0cmd();
		}
		tm.startTest();

		tm.printAllTexts();
		if (!tm.checkCS())
			System.out.println("Error!! :some ChannelSimulator still have packet!");
		if (!tm.checkEquality())
			System.out.println("Error!! :all Editor's text is NOT mutch!");
	}

	private void startTest() {
		for (EditorSimulator ee: editors){
			ee.start();
		}

		for (EditorSimulator ee: editors){
			try {
				ee.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void test2cmd(){
		for (int i=0; i<N_editor; i++){
			int j=0;
			LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
			// 各エディタが送信するコマンド列を生成

			String str = "created by Editor"+i+":"+j;
			REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
					0, i, j++,
					10, //Verify.random(text.size()-1), //size-1?
					str.length(), str);
			cmds.add( cmd);
			str = "created by Editor"+i+":"+j;
			cmd = new REPCommand(REP.REPCMD_INSERT,
					0, i, j++,
					10, //Verify.random(text.size()-1), //size-1?
					str.length(), str);
			cmds.add( cmd);

			EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i);
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}

	private void test1cmd(){
		for (int i=0; i<N_editor; i++){
			int j=0;
			LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
			//各エディタが送信するコマンド列を生成
			String str = "Editor"+i+":"+j;
			REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
					0, i, j++,
					10, //Verify.random(text.size()-1), //size-1?
					str.length(), str);
			cmds.add( cmd);
			EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i); 
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}

	private void test0cmd(){
		for (int i=0; i<N_editor; i++){
			LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
			//各エディタが送信するコマンド列を生成
			EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i); 
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}


	private void printAllTexts(){
		for(EditorSimulator ee: editors){
			System.out.println("--"+ee.getName()+"------------------------");
			ee.getText().printAllText();
		}
	}

	private boolean checkCS(){
		return ns.checkAllCS();
	}
	private boolean checkEquality(){
		/*
		Text ee0 = editors.remove().getText();
		return editors.remove().getText().equals(ee0);
		*/
		Text text0 = editors.element().getText();
		for(EditorSimulator ee: editors){
			if (!text0.equals(ee.getText())) return false;
		}
		return true;
	}
	
	private void test3cmd4editor(){
		for (int i=0; i<N_editor; i++){
			int j=0;
			LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();

			if(i==3||i==2||i==1){
			//各エディタが送信するコマンド列を生成
			String str = "Editor"+i+":"+j;
			REPCommand cmd = new REPCommand(REP.REPCMD_INSERT, 0, i, j++, 10, str.length(), str);
			cmds.add( cmd);
/*			str = "Editor"+i+":"+j;
			cmd = new REPCommand(REP.REPCMD_INSERT, 0, i, j++, 10, str.length(), str);
			cmds.add( cmd);
			str = "Editor"+i+":"+j;
			cmd = new REPCommand(REP.REPCMD_INSERT, 0, i, j++, 10, str.length(), str);
			cmds.add( cmd);
			str = "Editor"+i+":"+j;
			cmd = new REPCommand(REP.REPCMD_INSERT, 0, i, j++, 10, str.length(), str);
			cmds.add( cmd);
*/
			}

			EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i); 
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}
}