@Before public void setUp() { gap = new Gap(); gap.addValidAnswers("val1", "val2"); gap.addInvalidAnswers("inval1", "inval2", "inval3"); gap2 = new Gap(); gap2 = Gap.with(Sets.newHashSet("val1", "val2"), Sets.newHashSet("inval1", "inval2", "inval3")); gap2.addValidAnswers("val1", "val2"); }
/* * insertGapCore0w() can handle a long code sequence more than 32K. * It guarantees that the length of the inserted gap (NOPs) is equal to * gapLength. No other NOPs except some NOPs following TABLESWITCH or * LOOKUPSWITCH will not be inserted. * * Note: currentPos might be moved. * * @param where It must indicate the first byte of an opcode. * @param newWhere It contains the updated index of the position where a gap * is inserted and the length of the gap. * It must not be null. */ private byte[] insertGapCore0w( byte[] code, int where, int gapLength, boolean exclusive, ExceptionTable etable, CodeAttribute ca, Gap newWhere) throws BadBytecode { if (gapLength <= 0) return code; ArrayList jumps = makeJumpList(code, code.length); Pointers pointers = new Pointers(currentPos, mark, where, etable, ca); byte[] r = insertGap2w(code, where, gapLength, exclusive, jumps, pointers); currentPos = pointers.cursor; mark = pointers.mark; int where2 = pointers.mark0; if (where2 == currentPos && !exclusive) currentPos += gapLength; if (exclusive) where2 -= gapLength; newWhere.position = where2; newWhere.length = gapLength; return r; }
public Gap getGapByID(String id) { for (int i = 0; i < fGapList.size(); i++) { Gap aGap = (Gap) fGapList.getBasicElementAt(i); if (aGap.getId().equals(id)) return aGap; } return null; }
public Element toJDOM() { Element aInteractionElement = super.toJDOM(); aInteractionElement.setAttribute(AssessmentElementFactory.SHUFFLE, fShuffle ? "true" : "false"); for (int i = 0; i < fGapTextList.size(); i++) { Element aGapTextElement = ((GapText) fGapTextList.getBasicElementAt(i)).toJDOM(); if (aGapTextElement != null) aInteractionElement.addContent(aGapTextElement); } Element aBlockElement = new Element(AssessmentElementFactory.P, getNamespace()); if (fGapList.size() <= 0) { aBlockElement.setText(data); aInteractionElement.addContent(aBlockElement); return aInteractionElement; } int lastEnd = 0; for (int i = 0; i < fGapList.size(); i++) { Gap aGap = (Gap) fGapList.getBasicElementAt(i); if (aGap != null) { Element aGapElement = aGap.toJDOM(); int start = aGap.getStart(); if (start == lastEnd) { aBlockElement.addContent(aGapElement); } else { Text aText = new Text(data.substring(lastEnd, start - 1)); aBlockElement.addContent(aText); aBlockElement.addContent(aGapElement); } lastEnd = start + aGap.getLength(); } } if (lastEnd < data.length()) { Text aText = new Text(data.substring(lastEnd)); aBlockElement.addContent(aText); } aInteractionElement.addContent(aBlockElement); return aInteractionElement; }
/** * Inserts an inclusive or exclusive gap in front of the instruction at the given index <code>pos * </code>. Branch offsets and the exception table in the method body are also updated. The * inserted gap is filled with NOP. The gap length may be extended to a multiple of 4. * * <p>Suppose that the instruction at the given index is at the beginning of a block statement. If * the gap is inclusive, then it is included within that block. If the gap is exclusive, then it * is excluded from that block. * * <p>The index at which the gap is actually inserted might be different from pos since some other * bytes might be inserted at other positions (e.g. to change <code>GOTO</code> to <code>GOTO_W * </code>). The index is available from the <code>Gap</code> object returned by this method. * * <p>Suppose that the gap is inserted at the position of the next instruction that would be * returned by <code>next()</code> (not the last instruction returned by the last call to <code> * next()</code>). The next instruction returned by <code>next()</code> after the gap is inserted * is still the same instruction. It is not <code>NOP</code> at the first byte of the inserted * gap. * * @param pos the index at which a gap is inserted. * @param length gap length. * @param exclusive true if exclusive, otherwise false. * @return the position and the length of the inserted gap. * @since 3.11 */ public Gap insertGapAt(int pos, int length, boolean exclusive) throws BadBytecode { /** cursorPos indicates the next bytecode whichever exclusive is true or false. */ Gap gap = new Gap(); if (length <= 0) { gap.position = pos; gap.length = 0; return gap; } byte[] c; int length2; if (bytecode.length + length > Short.MAX_VALUE) { // currentPos might change after calling insertGapCore0w(). c = insertGapCore0w( bytecode, pos, length, exclusive, get().getExceptionTable(), codeAttr, gap); pos = gap.position; length2 = length; // == gap.length } else { int cur = currentPos; c = insertGapCore0(bytecode, pos, length, exclusive, get().getExceptionTable(), codeAttr); // insertGapCore0() never changes pos. length2 = c.length - bytecode.length; gap.position = pos; gap.length = length2; if (cur >= pos) currentPos = cur + length2; if (mark > pos || (mark == pos && exclusive)) mark += length2; } codeAttr.setCode(c); bytecode = c; endPos = getCodeLength(); updateCursors(pos, length2); return gap; }
@Override public void merge() { while (_bins.size() > getMaxBins()) { Gap<T> smallestGap = _gaps.pollFirst(); Bin<T> newBin = smallestGap.getStartBin().combine(smallestGap.getEndBin()); Gap<T> followingGap = _binsToGaps.get(smallestGap.getEndBin().getMean()); if (followingGap != null) { _gaps.remove(followingGap); } _bins.remove(smallestGap.getStartBin().getMean()); _bins.remove(smallestGap.getEndBin().getMean()); _binsToGaps.remove(smallestGap.getStartBin().getMean()); _binsToGaps.remove(smallestGap.getEndBin().getMean()); updateGaps(newBin); _bins.put(newBin.getMean(), newBin); } }
public void addGap(Gap gap) { root.addContent(gap.asJDomElement()); }
@Test public void testWith_String_StringArr() { Gap gap = Gap.with("foo", "bar", "bar2"); assertThat(gap.getValidAnswers(), hasItem("foo")); assertThat(gap.getInvalidAnswers(), hasItems("bar", "bar2")); }
@Test public void testToString() { assertEquals("Gap{valid=[val1, val2], invalid=[inval3, inval2, inval1]}", gap.toString()); }
@Test public void testEquals() { assertEquals(gap2, gap); assertFalse(gap.equals(null)); }
@Test public void testHashCode() { assertEquals(gap2.hashCode(), gap.hashCode()); }
@Test public void testGetAllAnswers() { assertEquals( Sets.newHashSet("inval1", "inval2", "inval3", "val1", "val2"), gap.getAllAnswers()); }
@Test public void testGetValidAnswers() { assertEquals(Sets.newHashSet("val1", "val2"), gap.getValidAnswers()); }
public void fromJDOM(Element element) { super.fromJDOM(element); for (Object attribute : element.getAttributes()) { String tag = ((Attribute) attribute).getName(); String value = ((Attribute) attribute).getValue(); if (AssessmentElementFactory.IDENTIFIER.equals(tag)) { fID = value; } else if (AssessmentElementFactory.SHUFFLE.equals(tag)) { fShuffle = "true".equalsIgnoreCase(value); } } for (Object o : element.getChildren()) { Element child = (Element) o; String tag = child.getName(); // String value = child.getText(); if (tag.equals(AssessmentElementFactory.GAP_TEXT)) { GapText aGapText = new GapText(fAssessmentItem, this); aGapText.fromJDOM((Element) child); fGapTextList.addElement(aGapText); } else if (tag.equals(AssessmentElementFactory.P)) { List grandChildren = child.getContent(); Iterator iterator = grandChildren.iterator(); Text textString = new Text(""); while (iterator.hasNext()) { Object grandChild = iterator.next(); if (grandChild instanceof Element) { Element e = (Element) grandChild; if (e.getName().equals(AssessmentElementFactory.GAP)) { Gap aGap = new Gap(fAssessmentItem, this); aGap.fromJDOM((Element) grandChild); aGap.setStart(textString.getText().length() + 1); InterpretationValue aInterpretationValue = getResponseDeclaration().getCorrectResponse(); for (int i = 0; i < aInterpretationValue.size(); i++) { String valueData = aInterpretationValue.getValueAt(i).getData(); int index = valueData.indexOf(" "); if (index != -1) { String targetID = valueData.substring(index + 1); if (targetID.equals(aGap.getId())) { String sourceID = valueData.substring(0, index); GapText aGapText = getGapTextByID(sourceID); aGap.setData(aGapText.getData()); aGap.setMatchedGapText(aGapText); aGap.setLength(aGapText.getData().length()); if (!aGapText.getData().equals("")) { textString.append(" " + aGapText.getData()); } else { System.out.println("correct choice is an empty string"); } fGapList.addElement(aGap); } } } } } else if (grandChild instanceof Text) { textString.append((Text) grandChild); } } data = textString.getText(); } } }