001: /*
002: * ====================================================================
003: * Copyright (c) 2004 Marc Strapetz, marc.strapetz@smartsvn.com.
004: * All rights reserved.
005: *
006: * This software is licensed as described in the file COPYING, which
007: * you should have received as part of this distribution. Use is
008: * subject to license terms.
009: * ====================================================================
010: */
011:
012: package de.regnis.q.sequence;
013:
014: import java.util.*;
015: import junit.framework.*;
016:
017: import de.regnis.q.sequence.core.*;
018: import de.regnis.q.sequence.media.*;
019:
020: /**
021: * @author Marc Strapetz
022: */
023: public class QSequenceDifferenceCoreTest extends TestCase {
024:
025: // Static =================================================================
026:
027: public static String fillWithChar(String string, int totalLength,
028: char chr) {
029: if (string.length() == totalLength) {
030: return string;
031: }
032:
033: final StringBuffer buffer = new StringBuffer(totalLength);
034: buffer.append(string);
035: fillWithChar(buffer, totalLength, chr);
036: return buffer.toString();
037: }
038:
039: public static String fillWithChar(String string, int totalLength,
040: int startIndex, char chr) {
041: if (string.length() == totalLength) {
042: return string;
043: }
044:
045: final StringBuffer buffer = new StringBuffer(totalLength);
046: buffer.append(string);
047: fillWithChar(buffer, totalLength, startIndex, chr);
048: return buffer.toString();
049: }
050:
051: public static StringBuffer fillWithChar(StringBuffer buffer,
052: int totalLength, char chr) {
053: return fillWithChar(buffer, totalLength, -1, chr);
054: }
055:
056: public static StringBuffer fillWithChar(StringBuffer buffer,
057: int totalLength, int startIndex, char chr) {
058: buffer.ensureCapacity(totalLength);
059: if (startIndex >= 0 && startIndex < buffer.length()) {
060: while (buffer.length() < totalLength) {
061: buffer.insert(startIndex, chr);
062: }
063: } else {
064: while (buffer.length() < totalLength) {
065: buffer.append(chr);
066: }
067: }
068: return buffer;
069: }
070:
071: // Accessing ==============================================================
072:
073: public void test() throws QSequenceException {
074: test1("abcabba", "cbabac", "--c-bba", "cb-ba-");
075: test1("abcccd", "accccd", "a-cccd", "a-cccd");
076: test2("abbb", "abbbb", "abbb", "abbb-");
077: test2("abccd", "abccd", "abccd", "abccd");
078: test2("abccd*", "abccd", "abccd-", "abccd");
079: test2("*abccd", "abccd", "-abccd", "abccd");
080: test2("abc*cd", "abccd", "abc-cd", "abccd");
081: test2("abccd", "x", "-----", "-");
082: test2("abccd", "", "-----", "");
083: test2("", "", "", "");
084: test2("Howdy", "Rucola", "-o---", "---o--");
085: test2("*bc", "bc", "-bc", "bc"); // Special case, which results immediately in D = 1
086: test2("b*c", "bc", "b-c", "bc"); // Special case, which results immediately in D = 1
087: test2("bc*", "bc", "bc-", "bc"); // Special case, which results immediately in D = 1
088: test2("abc*defgh*i*", "a+bc+defg+h+i++", "abc-defgh-i-",
089: "a-bc-defg-h-i--");
090: }
091:
092: // Utils ==================================================================
093:
094: private void test2(String left, String right, String leftTest,
095: String rightTest) throws QSequenceException {
096: test1(left, right, leftTest, rightTest);
097: test1(right, left, rightTest, leftTest);
098: }
099:
100: private void test1(String left, String right, String leftTest,
101: String rightTest) throws QSequenceException {
102: final QSequenceTestMedia media = QSequenceTestMedia
103: .createCharacterMedia(left, right);
104: final QSequenceIntMedia cachingMedia = new QSequenceCachingMedia(
105: media, new QSequenceDummyCanceller());
106: final QSequenceDiscardingMedia discardingMedia = new QSequenceDiscardingMedia(
107: cachingMedia,
108: new QSequenceDiscardingMediaNoConfusionDectector(false),
109: new QSequenceDummyCanceller());
110: final List blocks = new QSequenceDifference(discardingMedia,
111: discardingMedia).getBlocks();
112:
113: final StringBuffer leftBuffer = new StringBuffer(left);
114: final StringBuffer rightBuffer = new StringBuffer(right);
115: for (int index = 0; index < blocks.size(); index++) {
116: final QSequenceDifferenceBlock block = (QSequenceDifferenceBlock) blocks
117: .get(index);
118:
119: final int leftFrom = block.getLeftFrom();
120: final int leftTo = block.getLeftTo();
121: if (leftFrom <= leftTo) {
122: leftBuffer.replace(leftFrom, leftTo + 1, fillWithChar(
123: "", leftTo - leftFrom + 1, '-'));
124: }
125:
126: final int rightFrom = block.getRightFrom();
127: final int rightTo = block.getRightTo();
128: if (rightFrom <= rightTo) {
129: rightBuffer.replace(rightFrom, rightTo + 1,
130: fillWithChar("", rightTo - rightFrom + 1, '-'));
131: }
132: }
133:
134: assertEquals(leftTest, leftBuffer.toString());
135: assertEquals(rightTest, rightBuffer.toString());
136: }
137: }
|