1 21 package com.db4o.test.soda; 22 23 import com.db4o.query.*; 24 import com.db4o.test.*; 25 26 public class SodaNumberCoercion { 27 private static final String DOUBLEFIELD = "_doubleValue"; 28 private static final String FLOATFIELD = "_floatValue"; 29 private static final String LONGFIELD = "_longValue"; 30 private static final String INTFIELD = "_intValue"; 31 private static final String SHORTFIELD = "_shortValue"; 32 private static final String BYTEFIELD = "_byteValue"; 33 private static final Float ROUNDFLOATVALUE = new Float (100); 34 private static final Double ROUNDDOUBLEVALUE = new Double (100); 35 private static final Long LONGVALUE = new Long (100); 36 private static final Integer INTVALUE = new Integer (100); 37 private static final Short SHORTVALUE = new Short ((short)100); 38 private static final Byte BYTEVALUE = new Byte ((byte)100); 39 40 public static class Thing { 41 public byte _byteValue; 42 public short _shortValue; 43 public int _intValue; 44 public long _longValue; 45 public float _floatValue; 46 public double _doubleValue; 47 48 public Thing(byte byteValue,short shortValue,int intValue,long longValue,float floatValue,double doubleValue) { 49 _byteValue=byteValue; 50 _shortValue=shortValue; 51 _intValue=intValue; 52 _longValue=longValue; 53 _floatValue=floatValue; 54 _doubleValue=doubleValue; 55 } 56 } 57 58 public void store() { 59 Test.deleteAllInstances(Thing.class); 60 Test.store(new Thing((byte)10,(short)10,10,10L,10f,10d)); 61 Test.store(new Thing((byte)100,(short)100,100,100L,100f,100d)); 62 Test.store(new Thing((byte)42,(short)42,42,42L,42f,42d)); 63 Test.store(new Thing((byte)111,(short)111,111,111L,111.11f,111.11d)); 64 } 65 66 public void testIntegerTypes() { 67 assertSingleCoercionResult(BYTEFIELD,SHORTVALUE); 68 assertSingleCoercionResult(BYTEFIELD,INTVALUE); 69 assertSingleCoercionResult(BYTEFIELD,LONGVALUE); 70 71 assertSingleCoercionResult(SHORTFIELD,BYTEVALUE); 72 assertSingleCoercionResult(SHORTFIELD,INTVALUE); 73 assertSingleCoercionResult(SHORTFIELD,LONGVALUE); 74 75 assertSingleCoercionResult(INTFIELD,BYTEVALUE); 76 assertSingleCoercionResult(INTFIELD,SHORTVALUE); 77 assertSingleCoercionResult(INTFIELD,LONGVALUE); 78 79 assertSingleCoercionResult(LONGFIELD,BYTEVALUE); 80 assertSingleCoercionResult(LONGFIELD,SHORTVALUE); 81 assertSingleCoercionResult(LONGFIELD,INTVALUE); 82 } 83 84 public void testFloatingPointTypes() { 85 assertSingleCoercionResult(FLOATFIELD,ROUNDDOUBLEVALUE); 86 assertSingleCoercionResult(DOUBLEFIELD,ROUNDFLOATVALUE); 87 } 88 89 public void testMixed() { 90 assertSingleCoercionResult(BYTEFIELD, ROUNDFLOATVALUE); 91 assertSingleCoercionResult(BYTEFIELD, ROUNDDOUBLEVALUE); 92 assertSingleCoercionResult(SHORTFIELD, ROUNDFLOATVALUE); 93 assertSingleCoercionResult(SHORTFIELD, ROUNDDOUBLEVALUE); 94 assertSingleCoercionResult(INTFIELD, ROUNDFLOATVALUE); 95 assertSingleCoercionResult(INTFIELD, ROUNDDOUBLEVALUE); 96 assertSingleCoercionResult(LONGFIELD, ROUNDFLOATVALUE); 97 assertSingleCoercionResult(LONGFIELD, ROUNDDOUBLEVALUE); 98 assertSingleCoercionResult(FLOATFIELD, BYTEVALUE); 99 assertSingleCoercionResult(FLOATFIELD, SHORTVALUE); 100 assertSingleCoercionResult(FLOATFIELD, INTVALUE); 101 assertSingleCoercionResult(FLOATFIELD, LONGVALUE); 102 assertSingleCoercionResult(DOUBLEFIELD, BYTEVALUE); 103 assertSingleCoercionResult(DOUBLEFIELD, SHORTVALUE); 104 assertSingleCoercionResult(DOUBLEFIELD, INTVALUE); 105 assertSingleCoercionResult(DOUBLEFIELD, LONGVALUE); 106 } 107 108 public void testRounding() { 109 assertCoercionResult(FLOATFIELD, new Double (111.11), 0); assertCoercionResult(FLOATFIELD, new Integer (111), 0); 111 assertCoercionResult(INTFIELD, new Float (111.11), 0); 112 } 113 114 public void testOverflow() { 115 long cmpval=((long)Integer.MAX_VALUE)-Integer.MIN_VALUE+1+INTVALUE.intValue(); 116 assertCoercionResult(INTFIELD, new Long (cmpval), 0); 117 } 118 119 private void assertSingleCoercionResult(String fieldName,Number value) { 120 assertCoercionResult(fieldName,value,1); 121 } 122 123 private void assertCoercionResult(String fieldName,Number value,int expCount) { 124 Query q = Test.query(); 125 q.constrain(Thing.class); 126 Constraint constr=q.descend(fieldName).constrain(value); 127 Test.ensure(constr!=null); 128 Test.ensureEquals(expCount, q.execute().size()); 129 } 130 } 131 | Popular Tags |