|
| 1 | +package org.numenta.nupic; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.joda.time.DateTime; |
| 11 | +import org.junit.Test; |
| 12 | +import org.numenta.nupic.encoders.DateEncoder; |
| 13 | +import org.numenta.nupic.util.Tuple; |
| 14 | + |
| 15 | + |
| 16 | +public class FieldMetaTypeTest { |
| 17 | + |
| 18 | + /** |
| 19 | + * Test decoding of 9 out of 10 types of {@link FieldMetaType}s. |
| 20 | + */ |
| 21 | + @Test |
| 22 | + public void testDecodeType() { |
| 23 | + List<String> linput = new ArrayList<>(); |
| 24 | + linput.add("test"); |
| 25 | + String loutput = FieldMetaType.LIST.decodeType(linput.toString(), null); |
| 26 | + assertEquals(linput.toString(), loutput); |
| 27 | + |
| 28 | + String catinput = "TestString"; |
| 29 | + String output = FieldMetaType.STRING.decodeType(catinput, null); |
| 30 | + assertEquals(catinput, output); |
| 31 | + |
| 32 | + String binput = "true"; |
| 33 | + Double boutput = FieldMetaType.BOOLEAN.decodeType(binput, null); |
| 34 | + assertEquals(new Double(1), boutput); |
| 35 | + |
| 36 | + String ginput = "100;200;5"; |
| 37 | + Tuple texpected = new Tuple(100.0, 200.0, 5.0); |
| 38 | + Tuple tupleOut = FieldMetaType.GEO.decodeType(ginput, null); |
| 39 | + assertEquals(texpected, tupleOut); |
| 40 | + |
| 41 | + String intinput = "1337"; |
| 42 | + double intoutput = FieldMetaType.INTEGER.decodeType(intinput, null); |
| 43 | + assertEquals(1337.0d, intoutput, 0); |
| 44 | + |
| 45 | + intoutput = FieldMetaType.FLOAT.decodeType(intinput, null); |
| 46 | + assertEquals(1337.0d, intoutput, 0); |
| 47 | + |
| 48 | + // DARR = Dense Array |
| 49 | + int[] dainput = { 1, 0, 1, 0 }; |
| 50 | + int[] daoutput = FieldMetaType.DARR.decodeType(Arrays.toString(dainput), null); |
| 51 | + assertTrue(Arrays.equals(dainput, daoutput)); |
| 52 | + |
| 53 | + // SARR = Sparse Array |
| 54 | + int[] sainput = { 0, 2 }; |
| 55 | + int[] saoutput = FieldMetaType.SARR.decodeType(Arrays.toString(sainput), null); |
| 56 | + assertTrue(Arrays.equals(sainput, saoutput)); |
| 57 | + |
| 58 | + DateTime comparison = new DateTime(2010, 11, 4, 13, 55, 01); |
| 59 | + String compareString = "2010-11-04 13:55:01"; |
| 60 | + // 3 bits for season, 1 bit for day of week, 3 for weekend, 5 for time of day |
| 61 | + // use of forced is not recommended, used here for readability. |
| 62 | + DateEncoder.Builder builder = DateEncoder.builder(); |
| 63 | + builder.formatPattern("yyyy-MM-dd HH:mm:ss"); |
| 64 | + |
| 65 | + DateEncoder de = builder.season(3) |
| 66 | + .dayOfWeek(1) |
| 67 | + .weekend(3) |
| 68 | + .timeOfDay(5).build(); |
| 69 | + |
| 70 | + DateTime dateOutput = FieldMetaType.DATETIME.decodeType(compareString, de); |
| 71 | + assertEquals(comparison, dateOutput); |
| 72 | + } |
| 73 | +} |
0 commit comments