  @Test
  public void testMerge() throws Exception {

    long now = System.currentTimeMillis() * 1000L;
    byte[] key = new byte[32];
    GTSEncoder encoder1 = new GTSEncoder(now - 1000000L, key);
    
    encoder1.addValue(now, 111L, 11L, new BigDecimal("1.11"));
    encoder1.addValue(now + 1000000L, 222L, 22L, new BigDecimal("2.22"));
    
    GTSEncoder encoder2 = new GTSEncoder(now - 500000L);
    
    encoder2.addValue(now, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("3.33"));
    encoder2.addValue(now + 500000L, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("4.44"));
    
    encoder1.merge(encoder2);
    
    GTSDecoder decoder = encoder1.getDecoder();
    
    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now, decoder.getTimestamp());
    Assert.assertEquals(111L, decoder.getLocation());
    Assert.assertEquals(11L, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("1.11"), decoder.getValue());
    
    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now + 1000000L, decoder.getTimestamp());
    Assert.assertEquals(222L, decoder.getLocation());
    Assert.assertEquals(22L, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("2.22"), decoder.getValue());

    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now, decoder.getTimestamp());
    Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation());
    Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("3.33"), decoder.getValue());

    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now + 500000L, decoder.getTimestamp());
    Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation());
    Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("4.44"), decoder.getValue());

    Assert.assertFalse(decoder.next());
    
    //
    // Now do the same with encoders which have the same base, triggering the use of the fastpath
    //
    
    encoder1 = new GTSEncoder(now - 1000000L, key);
    encoder1.addValue(now, 111L, 11L, new BigDecimal("1.11"));
    encoder1.addValue(now + 1000000L, 222L, 22L, new BigDecimal("2.22"));

    encoder2 = new GTSEncoder(now - 1000000L, key);
    encoder2.addValue(now, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("3.33"));
    encoder2.addValue(now + 500000L, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, new BigDecimal("4.44"));
    
    encoder1.merge(encoder2);

    decoder = encoder1.getDecoder();
    
    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now, decoder.getTimestamp());
    Assert.assertEquals(111L, decoder.getLocation());
    Assert.assertEquals(11L, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("1.11"), decoder.getValue());
    
    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now + 1000000L, decoder.getTimestamp());
    Assert.assertEquals(222L, decoder.getLocation());
    Assert.assertEquals(22L, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("2.22"), decoder.getValue());

    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now, decoder.getTimestamp());
    Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation());
    Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("3.33"), decoder.getValue());

    Assert.assertTrue(decoder.next());
    Assert.assertEquals(now + 500000L, decoder.getTimestamp());
    Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation());
    Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation());
    Assert.assertEquals(new BigDecimal("4.44"), decoder.getValue());

    Assert.assertFalse(decoder.next());
    
    
  }