def test_sort(): random.seed(42) rvals = pgf.var_int() rkeys = pgf.var_int() rsorted = pgf.sort(rvals, rkeys) lvals = [] lkeys = [] for _ in range(10): nItems = random.randint(75, 155) vals = list(range(nItems)) keys = list(range(nItems)) random.shuffle(vals) random.shuffle(keys) lvals.append(vals) lkeys.append(keys) expected = [ x for x, _ in sorted(zip(vals, keys), key=lambda pair: pair[1]) ] pgf.assign(rvals, vals) pgf.assign(rkeys, keys) assert tu.equal(expected, pgf.read(rsorted)) # Test the same with tree combinatorics. expected = [[x for x, _ in sorted(zip(v, k), key=lambda pair: pair[1])] for v, k in zip(lvals, lkeys)] pgf.assign(rvals, lvals) pgf.assign(rkeys, lkeys) assert tu.equal(expected, pgf.read(rsorted))
def test_flatten(): random.seed(42) intree = pgf.var_int() outtree = pgf.flatten(intree) for _ in range(5): vals = [[random.randint(23, 234)] for __ in range(10)] pgf.assign(intree, vals) expected = [item for subl in vals for item in subl] assert tu.equal(expected, pgf.read(outtree)) vals = [[[random.randint(23, 234) for __ in range(5)] for ___ in range(5)] for _____ in range(5)] pgf.assign(intree, vals) expected = [item for l0 in vals for l1 in l0 for item in l1] assert tu.equal(expected, pgf.read(outtree))
def test_graft(): random.seed(42) intree = pgf.var_int() out = pgf.graft(intree) for _ in range(5): vals = [random.randint(23, 345) for __ in range(10)] expected = [[v] for v in vals] pgf.assign(intree, vals) assert tu.equal(expected, pgf.read(out)) for _ in range(5): vals = [[random.randint(23, 345) for __ in range(5)] for ___ in range(5)] expected = [[[v] for v in vl] for vl in vals] pgf.assign(intree, vals) assert tu.equal(expected, pgf.read(out))
def test_treeInputs(): random.seed(42) vals = [[random.randint(23, 345) for _ in range(10)] for _ in range(10)] rvals = pgf.var_int() rsum = pgf.treeSum(rvals) expected = sum([sum(v) for v in vals]) pgf.assign(rvals, vals) assert tu.equal(expected, pgf.read(rsum))
def test_listSumInt(): random.seed(42) valrange = (23, 345) vals = [random.randint(valrange[0], valrange[1]) for _ in range(25)] lst = pgf.var_int(vals) result = pgf.listSum(lst) assert tu.equal(sum(vals), pgf.read(result))
def test_heterogenousTrees(): random.seed(42) vals1 = [[2], [3]] vals2 = [4, 5] expected = [[8, 10], [12, 15]] rvals1 = pgf.var_int(vals1) rvals2 = pgf.var_int(vals2) prod = pgf.mul(rvals1, rvals2) assert tu.equal(expected, pgf.read(prod))
def binaryIntOpTest(expFn, pgfn, minval=2, maxval=124): random.seed(42) a = pgf.var_int() b = pgf.var_int() r = pgfn(a, b) for _ in range(20): va = random.randint(minval, maxval) vb = random.randint(minval, maxval) expected = expFn(va, vb) pgf.assign(a, va) pgf.assign(b, vb) assert tu.equal(expected, pgf.read(r)) vas = [random.randint(minval, maxval) for _ in range(20)] vbs = [random.randint(minval, maxval) for _ in range(20)] expectedVals = [expFn(va, vb) for va, vb in zip(vas, vbs)] pgf.assign(a, vas) pgf.assign(b, vbs) assert tu.equal(expectedVals, pgf.read(r))
def test_itemListMapping(): random.seed(42) singleval = 42 single = pgf.var_int(singleval) lst = pgf.var_int() out = pgf.add(single, lst) for _ in range(10): vals = list(range(12)) random.shuffle(vals) expected = [singleval + v for v in vals] pgf.assign(lst, vals) assert tu.equal(expected, pgf.read(out))
def test_dispatch(): random.seed(42) valrange = (23, 345) rvals = pgf.var_int() rpattern = pgf.var_bool() rtvals, rfvals = pgf.dispatch(rvals, rpattern) for _ in range(20): nItems = 25 vals = [ random.randint(valrange[0], valrange[1]) for _ in range(nItems) ] pattern = [ random.randint(valrange[0], valrange[1]) % 2 == 0 for _ in range(nItems) ] tvals = [v for v, p in zip(vals, pattern) if p] fvals = [v for v, p in zip(vals, pattern) if not p] pgf.assign(rvals, vals) pgf.assign(rpattern, pattern) assert tu.equal(tvals, pgf.read(rtvals)) assert tu.equal(fvals, pgf.read(rfvals))
def test_seriesInt(): random.seed(42) valrange = (23, 256) steprange = (2, 9) valstart = random.randint(valrange[0], valrange[1]) valstep = random.randint(steprange[0], steprange[1]) valcount = random.randint(valrange[0], valrange[1]) valstop = valstart + valstep * valcount start = pgf.var_int(valstart) step = pgf.var_int(valstep) count = pgf.var_int(valcount) series = pgf.series(start, step, count) assert tu.equal(range(valstart, valstop, valstep), pgf.read(series))
def test_combinations(): random.seed(42) valrange = (23, 345) rvals = pgf.var_int() rnumc = pgf.var_int() rcombs = pgf.combinations(rvals, rnumc) for _ in range(5): nItems = random.randint(23, 29) numc = random.randint(3, 5) vals = [ random.randint(valrange[0], valrange[1]) for _ in range(nItems) ] combs = list(itertools.combinations(vals, numc)) pgf.assign(rvals, vals) pgf.assign(rnumc, numc) assert tu.equal(combs, pgf.read(rcombs))
def compareInt(a, b): return tu.equal(a, b)