01: /*
02: * ====================================================================
03: * Copyright (c) 2004 Marc Strapetz, marc.strapetz@smartsvn.com.
04: * All rights reserved.
05: *
06: * This software is licensed as described in the file COPYING, which
07: * you should have received as part of this distribution. Use is
08: * subject to license terms.
09: * ====================================================================
10: */
11:
12: package de.regnis.q.sequence;
13:
14: import java.util.*;
15: import junit.framework.*;
16:
17: import de.regnis.q.sequence.core.*;
18:
19: /**
20: * @author Marc Strapetz
21: */
22: public class QSequenceLCSTest extends TestCase {
23:
24: // Accessing ==============================================================
25:
26: public void test() throws QSequenceException {
27: test1("abcabba", "cbabac", "cbba");
28: test1("cbabac", "abcabba", "cbba");
29:
30: test2("abccd", "abccd", "abccd");
31: test2("ab*", "ab", "ab");
32: test2("*abccd", "abccd", "abccd");
33: test2("***abccd", "abccd", "abccd");
34: test2("abc*d", "abc+d", "abcd");
35: test2("abc*d", "abcd", "abcd");
36: test2("abc*d", "abc++d", "abcd");
37: test2("abc*d", "abc+++d", "abcd");
38: test2("abc*d", "ab+c++++d", "abcd");
39: test2("ab", "ccd", "");
40: test2("ab", "bccd", "b");
41: test2("******a*****b*****c*c***d", "abccd", "abccd");
42: test2("******a*****b*****c*c***d", "+a+b+c+c+d+", "abccd");
43:
44: test2("a", "***a", "a");
45: test2("a", "****a", "a");
46: test2("a", "a****", "a");
47: test2("", "", "");
48: test2("a", "", "");
49: test2("aaaaaaaaa", "", "");
50: test2("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
51: "a", "a");
52:
53: test2("This is a small test with some minor variations.",
54: "Thies ist a smal text with some minbr variation.",
55: "This is a smal tet with some minr variation.");
56: }
57:
58: // Utils ==================================================================
59:
60: private void test2(String left, String right, String lcs)
61: throws QSequenceException {
62: test1(left, right, lcs);
63: test1(right, left, lcs);
64: }
65:
66: private void test1(String left, String right, String lcs)
67: throws QSequenceException {
68: final QSequenceTestMedia media = QSequenceTestMedia
69: .createCharacterMedia(left, right);
70: final List commands = new QSequenceSimpleLCS(media)
71: .getCommands();
72: int lastLeft = -1;
73: int lastRight = -1;
74:
75: final StringBuffer calculatedLcs = new StringBuffer();
76: for (Iterator it = commands.iterator(); it.hasNext();) {
77: final QSequenceSimpleLCSCommand command = (QSequenceSimpleLCSCommand) it
78: .next();
79: if (command.isLeft()) {
80: assertTrue(lastLeft <= command.getFrom());
81: for (int index = command.getFrom(); index <= command
82: .getTo(); index++) {
83: calculatedLcs.append(media.getLeftLine(index));
84: }
85: lastLeft = command.getTo();
86: } else {
87: assertTrue(lastRight <= command.getFrom());
88: for (int index = command.getFrom(); index <= command
89: .getTo(); index++) {
90: calculatedLcs.append(media.getRightLine(index));
91: }
92: lastRight = command.getTo();
93: }
94: }
95:
96: assertEquals(lcs, calculatedLcs.toString());
97: }
98: }
|