如何在两个Dictionary <string,List <string >>对象之间进行值比较

use*_*174 5 c# unit-testing

我有以下数据结构:

Dictionary<string, List<string>>
Run Code Online (Sandbox Code Playgroud)

如何进行比较以确保两个不同对象之间的值相等?

即:

    Dictionary<string, List<string>> expected = new Dictionary<string, List<string>>();
    expected.Add("CREDIT", new List<string> { "K   R   EH   D   IH   T" });
    expected.Add("CARD", new List<string> { "K   AA   R   D" });

    Dictionary<string, List<string>> actual;
    actual = target.GetTermDictionary();
    if (!Enumerable.SequenceEqual(expected, actual))
    {
        Assert.Fail();
    }
Run Code Online (Sandbox Code Playgroud)

我不认为SequanceEqual在这里很好..

谢谢

Jon*_*nna 5

快速正确和错误的第一个捷径:

if(ReferenceEqual(actual, expected))
  return true;
if(actual == null || expected == null || actual.Count != expected.Count)
  return false;
Run Code Online (Sandbox Code Playgroud)

这也处理空检查,因此我们所做的其他任何事情都不能抛出空引用异常。如果您像示例中那样在创建后就有了它,则可以跳过所有此栏来比较计数,但如果您将其放在单独的方法中,则应保留它,以防万一。

我们不能只对两个字典调用 SequenceEqual,因为我们不能保证以相同的顺序获取键。对于其他类型的值,我们可以这样做:

return actual.OrderBy(kvp => kvp.Key).SequenceEqual(expected.OrderBy(kvp => kvp.Key));
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为两个序列相等的List<string>值不会被视为等于DefaultEqualityComparer<List<string>>.Equals()将调用的方法。

IEqualityComparer<KeyValuePair<string, List<string>>>如果我们对使用 SequenceEqual 非常不满,我们可以创建一个,但使用非 Linq 方法可能更简单,尽管 Linq 通常更简单、更简洁(一旦您找到了方法)。因此:

List<string> expectedVal;
foreach(KeyValuePair<string, List<string> kvp in actual)
{
  if(!expected.TryGetValue(kvp.key, out expectedVal) || kvp.Value.Count != expectedVal.Count || !kvp.Value.SequenceEquals(expectedVal))
    return false;

}
return true;
Run Code Online (Sandbox Code Playgroud)

变体可以处理不同的平等观点。例如,kvp.Value.OrderBy(x => x).SequenceEquals(expectedVal.OrderBy(x => x))如果我们想将不同顺序的相同项目的两个列表视为相等,则可以使用。

总而言之,很多东西在一起:

if(ReferenceEqual(actual, expected))
  return true;
if(actual == null || expected == null || actual.Count != expected.Count)
  return false;
List<string> expectedVal;
foreach(KeyValuePair<string, List<string> kvp in actual)
{
  if(!expected.TryGetValue(kvp.key, out expectedVal) || kvp.Value.Count != expectedVal.Count || !kvp.Value.SequenceEquals(expectedVal))
    return false;

}
return true;
Run Code Online (Sandbox Code Playgroud)

编辑:只是为了好玩,使用 SequenceEquals 的方式:

internal class KvpSLSEq : IEqualityComparer<KeyValuePair<string, List<string>>>
{
  public bool Equals(KeyValuePair<string, List<string>> x, KeyValuePair<string, List<string>> y)
  {
    return x.Key == y.Key && x.Value.Count == y.Value.Count && x.Value.SequenceEquals(y.Value);
  }
  public int GetHashCode(KeyValuePair<string, List<string>> obj)
  {
    //you could just throw NotImplementedException unless you'll reuse this elsewhere.
    int hash = obj.Key.GetHashCode;
    foreach(string val in obj.Value)
       hash = hash * 31 + (val == null ? 0 : val.GetHashCode());
  }
}
Run Code Online (Sandbox Code Playgroud)

完成后我们可以使用简洁的:

actual.OrderBy(kvp => kvp.Key).SequenceEqual(expected.OrderBy(kvp => kvp.Key), new KvpSLSEq());
Run Code Online (Sandbox Code Playgroud)

但只有当 KvpSLSEq 也在其他地方使用时,它才真正简洁。