views:

397

answers:

2

I have a custom implementation of the Map interface which does some fancy stuff, like lazy evaluation of functions. the implementation should appear immutable after construction from outside (e.g. no put() and putAll() methods are supported)

I it looks like it works in the most basic conditions. Since it is quite complex, i am sure there must be tons of lurking bugs w.r.t thread safety, irregular order of operations and much more..

Since the contract of the Map interface is well-defined i am sure there must exist a generic test collection which checks corner-cases, thread safety etc.

I have heard that Google Collections runs about 25000 unit tests for their library. Is it possible to download them somewhere?

+4  A: 

The Google Collections zip contains their tests. There should be a google-collect-testfw jar in there.

Specifically, there's an abstract test for the general contract of Map.

Adam Jaskiewicz
Dig around in svn. There looks to be plenty of promising stuff in there.
Adam Jaskiewicz
A: 

You might want to see if Google Collections has something that meets your needs so you don't have to support your own Map. See, for instance, MapMaker

private Map<Key, Graph> createMap() {
  ConcurrentMap<Key, Graph> graphs = new MapMaker()
     .concurrencyLevel(32)
     .softKeys()
     .weakValues()
     .expiration(30, TimeUnit.MINUTES)
     .makeComputingMap(
        new Function<Key, Graph>() {
          public Graph apply(Key key) {
            return createExpensiveGraph(key);
          }
        });
  return Collections.unmodifiableMap(graphs);
}

Note that the map won't appear completely immutable after construction, since doing a map.get(key) with a previously-unseen key will change what is seen by Map.entrySet()

If you need to write a custom Map implementation and want to good place to start for your tests, Adam's suggestion of using Google Collections's MapInterfaceTest is a good one.

NamshubWriter
in my case its the other way round value is given, compute key
Andreas Petersson