@Test
@SuppressWarnings("unchecked")
public void testSortedMapWritable() {

  Text[] keys = {
      new Text("key1"),
      new Text("key2"),
      new Text("key3"),
  };
  
  BytesWritable[] values = {
      new BytesWritable("value1".getBytes()),
      new BytesWritable("value2".getBytes()),
      new BytesWritable("value3".getBytes())
  };
  SortedMapWritable<Text> inMap = new SortedMapWritable<Text>();
  for (int i = 0; i < keys.length; i++) {
    inMap.put(keys[i], values[i]);
  }
  
  assertEquals(0, inMap.firstKey().compareTo(keys[0]));
  assertEquals(0, inMap.lastKey().compareTo(keys[2]));
  SortedMapWritable<Text> outMap = new SortedMapWritable<Text>(inMap);
  assertEquals(inMap.size(), outMap.size());
  
  for (Map.Entry<Text, Writable> e: inMap.entrySet()) {
    assertTrue(outMap.containsKey(e.getKey()));
    WritableComparable<WritableComparable<?>> aValue = (WritableComparable<WritableComparable<?>>) outMap.get(e.getKey());
    WritableComparable<WritableComparable<?>> bValue = (WritableComparable<WritableComparable<?>>) e.getValue();
    assertEquals(0, aValue.compareTo(bValue));
  }
  
  // Now for something a little harder...
  
  Text[] maps = {
      new Text("map1"),
      new Text("map2")
  };
  
  SortedMapWritable<Text> mapOfMaps = new SortedMapWritable<Text>();
  mapOfMaps.put(maps[0], inMap);
  mapOfMaps.put(maps[1], outMap);
  
  SortedMapWritable<Text> copyOfMapOfMaps = new SortedMapWritable<Text>(mapOfMaps);
  for (int i = 0; i < maps.length; i++) {
    assertTrue(copyOfMapOfMaps.containsKey(maps[i]));
    SortedMapWritable<Text> a = (SortedMapWritable<Text>) mapOfMaps.get(maps[i]);
    SortedMapWritable<Text> b = (SortedMapWritable<Text>) copyOfMapOfMaps.get(maps[i]);
    assertEquals(a.size(), b.size());
    for (Writable key: a.keySet()) {
      assertTrue(b.containsKey(key));
      
      // This will work because we know what we put into each set
      
      WritableComparable<WritableComparable<?>> aValue = (WritableComparable<WritableComparable<?>>) a.get(key);
      WritableComparable<WritableComparable<?>> bValue = (WritableComparable<WritableComparable<?>>) b.get(key);
      assertEquals(0, aValue.compareTo(bValue));
    }
  }
}