java.util.Map的通用测试工具?

时间:2021-07-14 16:25:47

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)

我有一个Map接口的自定义实现,它做了一些奇特的东西,比如懒惰的函数评估。从外部构造后,实现应该是不可变的(例如,不支持put()和putAll()方法)

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.

由于Map接口的合约定义明确,我确信必须存在一个通用的测试集合,用于检查角落情况,线程安全等。

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

我听说Google Collections为他们的库运行了大约25000个单元测试。可以在某处下载它们吗?

2 个解决方案

#1


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

Google Collections zip包含他们的测试。那里应该有一个google-collect-testfw jar。

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

具体来说,有一个Map的一般合同的抽象测试。

#2


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

您可能想看看Google Collections是否有满足您需求的内容,因此您无需支持自己的地图。例如,请参阅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()

请注意,构造后映射不会显示为完全不可变,因为使用以前看不见的键执行map.get(key)将更改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.

如果您需要编写自定义Map实现并想要开始测试的好地方,Adam建议使用Google Collections的MapInterfaceTest是一个很好的建议。

#1


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

Google Collections zip包含他们的测试。那里应该有一个google-collect-testfw jar。

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

具体来说,有一个Map的一般合同的抽象测试。

#2


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

您可能想看看Google Collections是否有满足您需求的内容,因此您无需支持自己的地图。例如,请参阅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()

请注意,构造后映射不会显示为完全不可变,因为使用以前看不见的键执行map.get(key)将更改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.

如果您需要编写自定义Map实现并想要开始测试的好地方,Adam建议使用Google Collections的MapInterfaceTest是一个很好的建议。