数据流覆盖范围(Data Flow Coverage)
如果您编写程序,通常可以驱动程序以覆盖所有路径。 因此,100%的覆盖率很容易获得(忽略现代编译器无法捕获的不可行的代码路径)。
但是,100%的代码覆盖率应该意味着所有变量定义使用覆盖率也都会实现,因为变量是在程序中定义的并在其中使用。 如果涵盖所有代码,则还应涵盖所有DU对。
那么,为什么它说路径覆盖更容易获得,但数据流覆盖通常不可能达到100%? 我不明白为什么不呢? 有什么例子呢?
If you write a program, it is usually possible to drive it such that all paths are covered. Hence, 100% coverage is easy to obtain (ignoring unfeasible code paths which modern compilers catch anyways).
However, 100% code coverage should imply that all variable definition-use coverage is also achieved, because variables are defined within the program and used within it. If all code is covered, all DU pairs should also be covered.
Why then, is it said that path coverage is easier to obtain, but data flow coverage is not usually possible to achieve 100% ? I do not understand why not? What can be an example of that?
原文:https://stackoverflow.com/questions/19884711
最满意答案
当您实际只需要一个字典时,您当前正在将其反序列化为字典列表。
可能有一种更简洁的方法,但您可以将其反序列化为键值对列表,然后将其转换为字典。 示例代码:
using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; public class Test { static void Main() { string json = "[{'Key':'x','Value':'y'},{'Key':'a','Value':'b'}]"; var dictionary = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(json) .ToDictionary(pair => pair.Key, pair => pair.Value); Console.WriteLine(dictionary["x"]); // y Console.WriteLine(dictionary["a"]); // b } }
You're currently deserializing it as a list of dictionaries, when you only actually want a single dictionary.
There may be a cleaner way of doing this, but you can deserialize it as a list of key-value pairs, then convert that into a dictionary. Sample code:
using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; public class Test { static void Main() { string json = "[{'Key':'x','Value':'y'},{'Key':'a','Value':'b'}]"; var dictionary = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(json) .ToDictionary(pair => pair.Key, pair => pair.Value); Console.WriteLine(dictionary["x"]); // y Console.WriteLine(dictionary["a"]); // b } }
相关问答
更多-
词典词典(Dictionary of dictionaries)[2022-12-09]
所以答案结果是编译器更好地了解所涉及的类型。 省略说明者使它工作,虽然为什么我的说法者错了我还没有得到。 var ret = rs.OfType() .Where(x => x.Key.ToString().StartsWith("Title")) .ToDictionary( k => k.Value.ToString(), v => rs.OfType () .Where(x => x.Key.ToSt ... -
解决这个问题的一种方法是为基于字典的类使用定制的JsonConverter 。 代码实际上非常简单。 public class CustomDictionaryConverter
: JsonConverter { private string KeyPropertyName { get; set; } private string ValuePropertyName { get; set; } public CustomDictionaryConverter(stri ... -
你有一个多嵌套字典。 尝试: for i in oxford_dict["results"]: for j in i["lexicalEntries"]: for k in j["entries"]: for v in k["senses"]: print(v["definitions"]) 输出: ['a playing card with a single spot on it, ranked as the highest ...
-
将Dictionary转换为JSOn(convert Dictionary into JSOn)[2023-05-26]
使用Json.NET和JsonConvert http://james.newtonking.com/projects/json/help/SerializingJSON.html 没关系字典或对象是序列化到Json Use Json.NET and JsonConvert http://james.newtonking.com/projects/json/help/SerializingJSON.html It doesn't matter dictionary or object are seriali ... -
C#Json到对象词典(C# Json To Dictionary of Objects)[2022-07-28]
这可以通过一点递归来完成。 我将把IsJson定义为学术演习。 :) DictionaryRecursiveDeserialize(string json) { var result = JsonConvert.DeserializeObject >(json); foreach (var pair in result.ToArray()) { if(IsJson(pair. ... -
JSON到词典c#(JSON to Dictionary c#)[2023-09-19]
这个JSON似乎没有描述字典。 相反,它似乎描述了一个包含一个字典的字典列表,其键是“CRMNextFields”,其值是另一个字典列表。 因此,反序列化到这个工作: var data = JsonConvert .DeserializeObject- >>>>
(/*your json*/);
并访问内部词典,如下所示: var crmFields = data.First()["CRMNextFi ...
-
我认为你所寻找的是一种序列化关系的方法。 看一下: https://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers#Relations 另请注意,自Django 1.5起,simplejson已弃用: https://code.djangoproject.com/ticket/18023#comment:10 我创建了一个版本的Wad of stuff序列化程序,它与Django 1.5一起使用: https://github.com/kolb ...
-
您可以使用JavaScriptSerializer在json和Search类之间进行序列化/反序列化。 You can use JavaScriptSerializer to serialize/deserialize between json and your Search class.
-
JSON到词典(JSON to Dictionary)[2024-03-06]
当您实际只需要一个字典时,您当前正在将其反序列化为字典列表。 可能有一种更简洁的方法,但您可以将其反序列化为键值对列表,然后将其转换为字典。 示例代码: using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; public class Test { static void Main() { string json = "[{'Key':'x','Val ... -
将字典转换为Json(Convert dictionary to Json)[2021-11-08]
我认为你得到一个空结果的主要原因是因为你返回的是Result ,而不是你填充的dictionary变量。 另外,如果collListItem是IEnumerable(因为它似乎是,因为你用foreach循环它),你不需要逐项构建字典。 尝试: Dictionary dictionary=collListItem.ToDictionary(c=>c["ID"],c=>c["Title"]); return Json(dictionary,JsonReque ...