forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

sample2.py66 lines, 2.4 KiBHistoryRawDownload
1# usage: sample2.py OUT TEST N SEED -> OUT/s-TEST.json
2# Seeded sample of mutants whose containing fast-suite function is in U(TEST) (the functions TEST
3# covers that the fast suite does not). Writes the mutant names; prints eligible/sampled counts.
4import json, random, sys, collections
5out, test, n, seed = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4])
6
7def tail(p):
8 i = p.find("ec-h265/")
9 return p[i + 8:] if i >= 0 else None
10
11def load(name):
12 return json.load(open(f"{out}/{name}.json"))["data"][0]
13
14def funcs(data):
15 res = {}
16 for fn in data["functions"]:
17 if not fn["regions"]:
18 continue
19 fl = tail(fn["filenames"][fn["regions"][0][5]])
20 if fl is None:
21 continue
22 lo = min(r[0] for r in fn["regions"])
23 hi = max(r[2] for r in fn["regions"])
24 key = (fl, fn["name"])
25 d = res.setdefault(key, {"lo": lo, "hi": hi, "cov": False})
26 d["lo"] = min(d["lo"], lo)
27 d["hi"] = max(d["hi"], hi)
28 d["cov"] = d["cov"] or any(r[4] > 0 for r in fn["regions"])
29 return res
30
31fast = funcs(load("fast"))
32T = funcs(load(test))
33U = {(x[0], x[1]) for x in json.load(open(f"{out}/coverage-summary.json"))["per_test"][test]["U_functions"]}
34by_file = collections.defaultdict(list)
35for key in fast:
36 by_file[key[0]].append(key)
37
38elig = []
39covered = []
40for m in json.load(open(f"{out}/mutants.json")):
41 f = tail(m["file"]) if "ec-h265/" in m["file"] else m["file"]
42 line = m["span"]["start"]["line"]
43 best = None
44 for key in by_file.get(f, []):
45 fn = fast[key]
46 if not T.get(key, {}).get("cov"):
47 continue
48 if fn["lo"] <= line <= fn["hi"]:
49 if best is None or (fn["hi"] - fn["lo"]) < (best[1]["hi"] - best[1]["lo"]):
50 best = (key, fn)
51 if best is None:
52 continue
53 key = best[0]
54 if key in U and T.get(key, {}).get("cov"):
55 elig.append(m["name"])
56 elif T.get(key, {}).get("cov"):
57 covered.append(m["name"])
58
59elig = sorted(set(elig))
60covered = sorted(set(covered) - set(elig))
61# Fallback when U(T) holds no source functions (e.g. only test-file functions): mutants in functions
62# T covers, which the protocol's step 3 allows.
63pool, label = (elig, "U") if elig else (covered, "covered-fallback")
64picked = random.Random(seed).sample(pool, min(n, len(pool)))
65json.dump(picked, open(f"{out}/s-{test}.json", "w"))
66print(test, label, len(pool), "sampled", len(picked), "seed", seed)