Javi Merino | e4d10d7 | 2015-06-22 16:43:04 +0100 | [diff] [blame] | 1 | # $Copyright: |
| 2 | # ---------------------------------------------------------------- |
| 3 | # This confidential and proprietary software may be used only as |
| 4 | # authorised by a licensing agreement from ARM Limited |
| 5 | # (C) COPYRIGHT 2015 ARM Limited |
| 6 | # ALL RIGHTS RESERVED |
| 7 | # The entire notice above must be reproduced on all authorised |
| 8 | # copies and copies may only be made to the extent permitted |
| 9 | # by a licensing agreement from ARM Limited. |
| 10 | # ---------------------------------------------------------------- |
| 11 | # File: test_constraint.py |
| 12 | # ---------------------------------------------------------------- |
| 13 | # $ |
| 14 | # |
| 15 | |
| 16 | import pandas as pd |
| 17 | import unittest |
| 18 | |
| 19 | from cr2.plotter import AttrConf |
| 20 | from cr2.plotter.Constraint import ConstraintManager |
| 21 | |
| 22 | class TestConstraintManager(unittest.TestCase): |
| 23 | """Test cr2.plotter.ConstraintManager""" |
| 24 | |
| 25 | def __init__(self, *args, **kwargs): |
| 26 | """Init some common data for the tests""" |
| 27 | |
| 28 | self.dfrs = [pd.DataFrame({"load": [1, 2, 2, 3], |
| 29 | "freq": [2, 3, 3, 4], |
| 30 | "cpu": [0, 1, 0, 1]}), |
| 31 | pd.DataFrame({"load": [2, 3, 2, 1], |
| 32 | "freq": [1, 2, 2, 1], |
| 33 | "cpu": [1, 0, 1, 0]})] |
| 34 | super(TestConstraintManager, self).__init__(*args, **kwargs) |
| 35 | |
| 36 | def test_one_constraint(self): |
| 37 | """Test that the constraint manager works with one constraint""" |
| 38 | |
| 39 | dfr = self.dfrs[0] |
| 40 | |
| 41 | c_mgr = ConstraintManager(dfr, "load", None, AttrConf.PIVOT, {}) |
| 42 | |
| 43 | self.assertEquals(len(c_mgr.constraints), 1) |
| 44 | |
| 45 | series = c_mgr.constraints[0].result[AttrConf.PIVOT_VAL] |
| 46 | self.assertEquals(series.to_dict().values(), |
| 47 | dfr["load"].to_dict().values()) |
| 48 | |
| 49 | def test_no_pivot_multiple_runs(self): |
| 50 | """Test that the constraint manager works with multiple runs and no pivots""" |
| 51 | |
| 52 | c_mgr = ConstraintManager(self.dfrs, "load", None, AttrConf.PIVOT, {}) |
| 53 | |
| 54 | self.assertEquals(len(c_mgr.constraints), 2) |
| 55 | |
| 56 | for constraint, orig_dfr in zip(c_mgr.constraints, self.dfrs): |
| 57 | series = constraint.result[AttrConf.PIVOT_VAL] |
| 58 | self.assertEquals(series.to_dict().values(), |
| 59 | orig_dfr["load"].to_dict().values()) |
| 60 | |
| 61 | def test_no_pivot_multiple_columns_and_runs(self): |
| 62 | """Test the constraint manager with multiple columns and runs""" |
| 63 | |
| 64 | cols = ["load", "freq"] |
| 65 | c_mgr = ConstraintManager(self.dfrs, cols, None, AttrConf.PIVOT, {}) |
| 66 | |
| 67 | self.assertEquals(len(c_mgr.constraints), 2) |
| 68 | |
| 69 | for constraint, orig_dfr, col in zip(c_mgr.constraints, self.dfrs, cols): |
| 70 | series = constraint.result[AttrConf.PIVOT_VAL] |
| 71 | self.assertEquals(series.to_dict().values(), |
| 72 | orig_dfr[col].to_dict().values()) |
| 73 | |
| 74 | def test_no_pivot_filters(self): |
| 75 | """Test the constraint manager with filters""" |
| 76 | |
| 77 | simple_filter = {"freq": [2]} |
| 78 | |
| 79 | c_mgr = ConstraintManager(self.dfrs, "load", None, AttrConf.PIVOT, |
| 80 | simple_filter) |
| 81 | |
| 82 | num_constraints = len(c_mgr.constraints) |
| 83 | self.assertEquals(num_constraints, 2) |
| 84 | |
| 85 | self.assertEquals(len(c_mgr.constraints[0].result), 1) |
| 86 | |
| 87 | series_second_frame = c_mgr.constraints[1].result[AttrConf.PIVOT_VAL] |
| 88 | self.assertEquals(series_second_frame.to_dict().values(), [3, 2]) |
| 89 | |
| 90 | def test_pivoted_data(self): |
| 91 | """Test the constraint manager with a pivot and one run""" |
| 92 | |
| 93 | c_mgr = ConstraintManager(self.dfrs[0], "load", None, "cpu", {}) |
| 94 | |
| 95 | self.assertEquals(len(c_mgr.constraints), 1) |
| 96 | |
| 97 | constraint = c_mgr.constraints[0] |
| 98 | results = dict([(k, v.to_dict().values()) for k, v in constraint.result.items()]) |
| 99 | expected_results = {0: [1, 2], 1: [2, 3]} |
| 100 | |
| 101 | self.assertEquals(results, expected_results) |
| 102 | |
| 103 | def test_pivoted_multirun(self): |
| 104 | """Test the constraint manager with a pivot and multiple runs""" |
| 105 | |
| 106 | c_mgr = ConstraintManager(self.dfrs, "load", None, "cpu", {}) |
| 107 | |
| 108 | self.assertEquals(len(c_mgr.constraints), 2) |
| 109 | |
| 110 | constraint = c_mgr.constraints[0] |
| 111 | self.assertEquals(constraint.result[0].to_dict().values(), [1, 2]) |
| 112 | |
| 113 | constraint = c_mgr.constraints[1] |
| 114 | self.assertEquals(constraint.result[1].to_dict().values(), [2, 2]) |
| 115 | |
| 116 | def test_pivoted_multiruns_multicolumns(self): |
| 117 | """Test the constraint manager with multiple runs and columns""" |
| 118 | |
| 119 | c_mgr = ConstraintManager(self.dfrs, ["load", "freq"], None, "cpu", {}) |
| 120 | self.assertEquals(len(c_mgr.constraints), 2) |
| 121 | |
| 122 | constraint = c_mgr.constraints[0] |
| 123 | self.assertEquals(constraint.result[1].to_dict().values(), [2, 3]) |
| 124 | |
| 125 | constraint = c_mgr.constraints[1] |
| 126 | self.assertEquals(constraint.result[0].to_dict().values(), [2, 1]) |
| 127 | |
| 128 | def test_pivoted_with_filters(self): |
| 129 | """Test the constraint manager with pivoted data and filters""" |
| 130 | |
| 131 | simple_filter = {"load": [2]} |
| 132 | c_mgr = ConstraintManager(self.dfrs[0], "freq", None, "cpu", |
| 133 | simple_filter) |
| 134 | |
| 135 | self.assertEquals(len(c_mgr.constraints), 1) |
| 136 | |
| 137 | constraint = c_mgr.constraints[0] |
| 138 | result = constraint.result |
| 139 | |
| 140 | self.assertEquals(result[0].iloc[0], 3) |
| 141 | self.assertEquals(result[1].iloc[0], 3) |