001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: NewRegressionSeq.java,v 1.5 2008/01/02 12:07:04 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.regression;
008:
009: import java.util.*;
010:
011: import junit.framework.*;
012:
013: import com.hp.hpl.jena.rdf.model.*;
014: import com.hp.hpl.jena.regression.Regression.*;
015: import com.hp.hpl.jena.vocabulary.RDF;
016:
017: public class NewRegressionSeq extends NewRegressionBase {
018: public NewRegressionSeq(String name) {
019: super (name);
020: }
021:
022: public static Test suite() {
023: return new TestSuite(NewRegressionSeq.class);
024: }
025:
026: protected Model getModel() {
027: return ModelFactory.createDefaultModel();
028: }
029:
030: protected Model m;
031:
032: public void setUp() {
033: m = getModel();
034: }
035:
036: public void tearDown() {
037: m = null;
038: }
039:
040: public void testSeqAdd() {
041: Seq seq = m.createSeq();
042: assertEquals(0, seq.size());
043: assertTrue(m.contains(seq, RDF.type, RDF.Seq));
044: //
045: seq.add(tvBoolean);
046: assertTrue(seq.contains(tvBoolean));
047: assertFalse(seq.contains(!tvBoolean));
048: //
049: seq.add(tvByte);
050: assertTrue(seq.contains(tvByte));
051: assertFalse(seq.contains((byte) 101));
052: //
053: seq.add(tvShort);
054: assertTrue(seq.contains(tvShort));
055: assertFalse(seq.contains((short) 102));
056: //
057: seq.add(tvInt);
058: assertTrue(seq.contains(tvInt));
059: assertFalse(seq.contains((int) -101));
060: //
061: seq.add(tvLong);
062: assertTrue(seq.contains(tvLong));
063: assertFalse(seq.contains((long) -102));
064: //
065: seq.add(tvChar);
066: assertTrue(seq.contains(tvChar));
067: assertFalse(seq.contains('?'));
068: //
069: seq.add(123.456f);
070: assertTrue(seq.contains(123.456f));
071: assertFalse(seq.contains(456.123f));
072: //
073: seq.add(-123.456d);
074: assertTrue(seq.contains(-123.456d));
075: assertFalse(seq.contains(-456.123d));
076: //
077: seq.add("a string");
078: assertTrue(seq.contains("a string"));
079: assertFalse(seq.contains("a necklace"));
080: //
081: seq.add(m.createLiteral("another string"));
082: assertTrue(seq.contains("another string"));
083: assertFalse(seq.contains("another necklace"));
084: //
085: seq.add(new LitTestObj(12345));
086: assertTrue(seq.contains(new LitTestObj(12345)));
087: assertFalse(seq.contains(new LitTestObj(54321)));
088: //
089: Resource present = m.createResource(new ResTestObjF());
090: Resource absent = m.createResource(new ResTestObjF());
091: seq.add(present);
092: assertTrue(seq.contains(present));
093: assertFalse(seq.contains(absent));
094: //
095: assertEquals(12, seq.size());
096: }
097:
098: public void testSeqAddInts() {
099: final int num = 10;
100: Seq seq = m.createSeq();
101: for (int i = 0; i < num; i += 1)
102: seq.add(i);
103: assertEquals(num, seq.size());
104: List L = iteratorToList(seq.iterator());
105: assertEquals(num, L.size());
106: for (int i = 0; i < num; i += 1)
107: assertEquals(i, ((Literal) L.get(i)).getInt());
108: }
109:
110: public void testRemoveA() {
111: testRemove(bools("tttffffftt"));
112: }
113:
114: public void testRemoveB() {
115: testRemove(bools("ftftttttft"));
116: }
117:
118: public void testRemoveC() {
119: testRemove(bools("ffffffffff"));
120: }
121:
122: protected boolean[] bools(String s) {
123: boolean[] result = new boolean[s.length()];
124: for (int i = 0; i < s.length(); i += 1)
125: result[i] = s.charAt(i) == 't';
126: return result;
127: }
128:
129: protected void testRemove(boolean[] retain) {
130: final int num = retain.length;
131: Seq seq = m.createSeq();
132: for (int i = 0; i < num; i += 1)
133: seq.add(i);
134: //
135: List retained = new ArrayList();
136: //
137: NodeIterator nIter = seq.iterator();
138: for (int i = 0; i < num; i += 1) {
139: RDFNode x = nIter.nextNode();
140: if (retain[i])
141: retained.add(x);
142: else
143: nIter.remove();
144: }
145: //
146: assertFalse(nIter.hasNext());
147: assertEquals(retained, iteratorToList(seq.iterator()));
148: }
149:
150: public void testSeqAccessByIndexing() {
151: LitTestObj tvObject = new LitTestObj(12345);
152: Literal tvLiteral = m.createLiteral("test 12 string 2");
153: Resource tvResource = m.createResource();
154: Resource tvResObj = m.createResource(new ResTestObjF());
155: Object tvLitObj = new LitTestObj(1234);
156: Bag tvBag = m.createBag();
157: Alt tvAlt = m.createAlt();
158: Seq tvSeq = m.createSeq();
159: //
160: Seq seq = m.createSeq();
161: seq.add(true);
162: assertEquals(true, seq.getBoolean(1));
163: seq.add((byte) 1);
164: assertEquals((byte) 1, seq.getByte(2));
165: seq.add((short) 2);
166: assertEquals((short) 2, seq.getShort(3));
167: seq.add((int) -1);
168: assertEquals((int) -1, seq.getInt(4));
169: seq.add((long) -2);
170: assertEquals((long) -2, seq.getLong(5));
171: seq.add('!');
172: assertEquals('!', seq.getChar(6));
173: seq.add(123.456f);
174: assertEquals(123.456f, seq.getFloat(7), 0.00005);
175: seq.add(12345.67890);
176: assertEquals(12345.67890, seq.getDouble(8), 0.00000005);
177: seq.add("some string");
178: assertEquals("some string", seq.getString(9));
179: seq.add(tvLitObj);
180: assertEquals(tvLitObj, seq.getObject(10, new LitTestObjF()));
181: seq.add(tvResource);
182: assertEquals(tvResource, seq.getResource(11));
183: seq.add(tvResObj);
184: assertEquals(tvResObj, seq.getResource(12, new ResTestObjF()));
185: seq.add(tvLiteral);
186: assertEquals(tvLiteral, seq.getLiteral(13));
187: seq.add(tvBag);
188: assertEquals(tvBag, seq.getBag(14));
189: seq.add(tvAlt);
190: assertEquals(tvAlt, seq.getAlt(15));
191: seq.add(tvSeq);
192: assertEquals(tvSeq, seq.getSeq(16));
193: //
194: try {
195: seq.getInt(17);
196: fail("there is no element 17");
197: } catch (SeqIndexBoundsException e) {
198: pass();
199: }
200: try {
201: seq.getInt(0);
202: fail("there is no element 0");
203: } catch (SeqIndexBoundsException e) {
204: pass();
205: }
206: }
207:
208: public void testSeqInsertByIndexing() {
209: LitTestObj tvObject = new LitTestObj(12345);
210: Literal tvLiteral = m.createLiteral("test 12 string 2");
211: Resource tvResource = m.createResource();
212: Resource tvResObj = m.createResource(new ResTestObjF());
213: Object tvLitObj = new LitTestObj(1234);
214: Bag tvBag = m.createBag();
215: Alt tvAlt = m.createAlt();
216: Seq tvSeq = m.createSeq();
217:
218: Seq seq = m.createSeq();
219: seq.add(m.createResource());
220: seq.add(1, true);
221: assertEquals(true, seq.getBoolean(1));
222: seq.add(1, (byte) 1);
223: assertEquals((byte) 1, seq.getByte(1));
224: seq.add(1, (short) 2);
225: assertEquals((short) 2, seq.getShort(1));
226: seq.add(1, (int) -1);
227: assertEquals((int) -1, seq.getInt(1));
228: seq.add(1, (long) -2);
229: assertEquals((long) -2, seq.getLong(1));
230: seq.add(1, '!');
231: assertEquals('!', seq.getChar(1));
232: seq.add(1, 123.456f);
233: assertEquals(123.456f, seq.getFloat(1), 0.00005);
234: seq.add(1, 12345.67890);
235: assertEquals(12345.67890, seq.getDouble(1), 0.00000005);
236: seq.add(1, "some string");
237: assertEquals("some string", seq.getString(1));
238: seq.add(1, tvLitObj);
239: assertEquals(tvLitObj, seq.getObject(1, new LitTestObjF()));
240: seq.add(1, tvResource);
241: assertEquals(tvResource, seq.getResource(1));
242: seq.add(1, tvResObj);
243: assertEquals(tvResObj, seq.getResource(1, new ResTestObjF()));
244: seq.add(1, tvLiteral);
245: assertEquals(tvLiteral, seq.getLiteral(1));
246: seq.add(1, tvBag);
247: assertEquals(tvBag, seq.getBag(1));
248: seq.add(1, tvAlt);
249: assertEquals(tvAlt, seq.getAlt(1));
250: seq.add(1, tvSeq);
251: assertEquals(tvSeq, seq.getSeq(1));
252: //
253: assertEquals(0, seq.indexOf(1234543));
254: assertEquals(1, seq.indexOf(tvSeq));
255: assertEquals(2, seq.indexOf(tvAlt));
256: assertEquals(3, seq.indexOf(tvBag));
257: assertEquals(4, seq.indexOf(tvLiteral));
258: assertEquals(6, seq.indexOf(tvResource));
259: assertEquals(7, seq.indexOf(tvLitObj));
260: assertEquals(8, seq.indexOf("some string"));
261: assertEquals(9, seq.indexOf(12345.67890));
262: assertEquals(10, seq.indexOf(123.456f));
263: assertEquals(11, seq.indexOf('!'));
264: assertEquals(12, seq.indexOf((long) -2));
265: assertEquals(13, seq.indexOf((int) -1));
266: assertEquals(14, seq.indexOf((short) 2));
267: assertEquals(15, seq.indexOf((byte) 1));
268: assertEquals(16, seq.indexOf(true));
269: }
270:
271: public void testMoreIndexing() {
272: final int num = 10;
273: Seq seq = m.createSeq();
274: for (int i = 0; i < num; i += 1)
275: seq.add(i);
276:
277: try {
278: seq.add(0, false);
279: fail("cannot at at position 0");
280: } catch (SeqIndexBoundsException e) {
281: pass();
282: }
283:
284: seq.add(num + 1, false);
285: assertEquals(num + 1, seq.size());
286:
287: seq.remove(num + 1);
288: try {
289: seq.add(num + 2, false);
290: fail("cannot add past the end");
291: } catch (SeqIndexBoundsException e) {
292: pass();
293: }
294:
295: int size = seq.size();
296: for (int i = 1; i <= num - 1; i += 1) {
297: seq.add(i, 1000 + i);
298: assertEquals(1000 + i, seq.getInt(i));
299: assertEquals(0, seq.getInt(i + 1));
300: assertEquals(size + i, seq.size());
301: assertEquals(num - i - 1, seq.getInt(size));
302: }
303: }
304:
305: public void testSet() {
306: Model m = getModel();
307:
308: String test = "Test16";
309: int n = 0;
310:
311: NodeIterator nIter;
312: StmtIterator sIter;
313: Literal tvLiteral = m.createLiteral("test 12 string 2");
314: Resource tvResource = m.createResource();
315: Resource tvResObj = m.createResource(new ResTestObjF());
316: Bag tvBag = m.createBag();
317: Alt tvAlt = m.createAlt();
318: Seq tvSeq = m.createSeq();
319: int num = 10;
320:
321: Seq seq = m.createSeq();
322: for (int i = 0; i < num; i++)
323: seq.add(i);
324:
325: seq.set(5, tvBoolean);
326: assertEquals(tvBoolean, seq.getBoolean(5));
327: assertEquals(3, seq.getInt(4));
328: assertEquals(5, seq.getInt(6));
329: assertEquals(num, seq.size());
330:
331: seq.set(5, tvByte);
332: assertEquals(tvByte, seq.getByte(5));
333: assertEquals(3, seq.getInt(4));
334: assertEquals(5, seq.getInt(6));
335: assertEquals(num, seq.size());
336:
337: seq.set(5, tvShort);
338: assertEquals(tvShort, seq.getShort(5));
339: assertEquals(3, seq.getInt(4));
340: assertEquals(5, seq.getInt(6));
341: assertEquals(num, seq.size());
342:
343: seq.set(5, tvInt);
344: assertEquals(tvInt, seq.getInt(5));
345: assertEquals(3, seq.getInt(4));
346: assertEquals(5, seq.getInt(6));
347: assertEquals(num, seq.size());
348:
349: seq.set(5, tvLong);
350: assertEquals(tvLong, seq.getLong(5));
351: assertEquals(3, seq.getInt(4));
352: assertEquals(5, seq.getInt(6));
353: assertEquals(num, seq.size());
354:
355: seq.set(5, tvString);
356: assertEquals(tvString, seq.getString(5));
357: assertEquals(3, seq.getInt(4));
358: assertEquals(5, seq.getInt(6));
359: assertEquals(num, seq.size());
360:
361: seq.set(5, tvBoolean);
362: assertEquals(tvBoolean, seq.getBoolean(5));
363: assertEquals(3, seq.getInt(4));
364: assertEquals(5, seq.getInt(6));
365: assertEquals(num, seq.size());
366:
367: seq.set(5, tvFloat);
368: assertEquals(tvFloat, seq.getFloat(5), 0.00005);
369: assertEquals(3, seq.getInt(4));
370: assertEquals(5, seq.getInt(6));
371: assertEquals(num, seq.size());
372:
373: seq.set(5, tvDouble);
374: assertEquals(tvDouble, seq.getDouble(5), 0.000000005);
375: assertEquals(3, seq.getInt(4));
376: assertEquals(5, seq.getInt(6));
377: assertEquals(num, seq.size());
378:
379: seq.set(5, tvLiteral);
380: assertEquals(tvLiteral, seq.getLiteral(5));
381: assertEquals(3, seq.getInt(4));
382: assertEquals(5, seq.getInt(6));
383: assertEquals(num, seq.size());
384:
385: seq.set(5, tvResource);
386: assertEquals(tvResource, seq.getResource(5));
387: assertEquals(3, seq.getInt(4));
388: assertEquals(5, seq.getInt(6));
389: assertEquals(num, seq.size());
390:
391: seq.set(5, tvLitObj);
392: assertEquals(tvLitObj, seq.getObject(5, new LitTestObjF()));
393: assertEquals(3, seq.getInt(4));
394: assertEquals(5, seq.getInt(6));
395: assertEquals(num, seq.size());
396:
397: seq.set(5, tvResObj);
398: assertEquals(tvResObj, seq.getResource(5, new ResTestObjF()));
399: assertEquals(3, seq.getInt(4));
400: assertEquals(5, seq.getInt(6));
401: assertEquals(num, seq.size());
402: }
403: }
404:
405: /*
406: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
407: * All rights reserved.
408: *
409: * Redistribution and use in source and binary forms, with or without
410: * modification, are permitted provided that the following conditions
411: * are met:
412: * 1. Redistributions of source code must retain the above copyright
413: * notice, this list of conditions and the following disclaimer.
414: * 2. Redistributions in binary form must reproduce the above copyright
415: * notice, this list of conditions and the following disclaimer in the
416: * documentation and/or other materials provided with the distribution.
417: * 3. The name of the author may not be used to endorse or promote products
418: * derived from this software without specific prior written permission.
419: *
420: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
421: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
422: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
423: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
424: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
425: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
426: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
427: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
428: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
429: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
430: */
|