xref: /aosp_15_r20/kernel/tests/net/test/parameterization_test.py (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/python3
2*2f2c4c7aSAndroid Build Coastguard Worker#
3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright 2018 The Android Open Source Project
4*2f2c4c7aSAndroid Build Coastguard Worker#
5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*2f2c4c7aSAndroid Build Coastguard Worker#
9*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0
10*2f2c4c7aSAndroid Build Coastguard Worker#
11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License.
16*2f2c4c7aSAndroid Build Coastguard Worker
17*2f2c4c7aSAndroid Build Coastguard Workerimport itertools
18*2f2c4c7aSAndroid Build Coastguard Workerimport unittest
19*2f2c4c7aSAndroid Build Coastguard Worker
20*2f2c4c7aSAndroid Build Coastguard Workerimport net_test
21*2f2c4c7aSAndroid Build Coastguard Workerimport util
22*2f2c4c7aSAndroid Build Coastguard Worker
23*2f2c4c7aSAndroid Build Coastguard Worker
24*2f2c4c7aSAndroid Build Coastguard Workerdef InjectTests():
25*2f2c4c7aSAndroid Build Coastguard Worker  ParmeterizationTest.InjectTests()
26*2f2c4c7aSAndroid Build Coastguard Worker
27*2f2c4c7aSAndroid Build Coastguard Worker
28*2f2c4c7aSAndroid Build Coastguard Worker# This test class ensures that the Parameterized Test generator in utils.py
29*2f2c4c7aSAndroid Build Coastguard Worker# works properly. It injects test methods into itself, and ensures that they
30*2f2c4c7aSAndroid Build Coastguard Worker# are generated as expected, and that the TestClosures being run are properly
31*2f2c4c7aSAndroid Build Coastguard Worker# defined, and running different parameterized tests each time.
32*2f2c4c7aSAndroid Build Coastguard Workerclass ParmeterizationTest(net_test.NetworkTest):
33*2f2c4c7aSAndroid Build Coastguard Worker  tests_run_list = []
34*2f2c4c7aSAndroid Build Coastguard Worker
35*2f2c4c7aSAndroid Build Coastguard Worker  @staticmethod
36*2f2c4c7aSAndroid Build Coastguard Worker  def NameGenerator(a, b, c):
37*2f2c4c7aSAndroid Build Coastguard Worker    return str(a) + "_" + str(b) + "_" + str(c)
38*2f2c4c7aSAndroid Build Coastguard Worker
39*2f2c4c7aSAndroid Build Coastguard Worker  @classmethod
40*2f2c4c7aSAndroid Build Coastguard Worker  def InjectTests(cls):
41*2f2c4c7aSAndroid Build Coastguard Worker    PARAMS_A = (1, 2)
42*2f2c4c7aSAndroid Build Coastguard Worker    PARAMS_B = (3, 4)
43*2f2c4c7aSAndroid Build Coastguard Worker    PARAMS_C = (5, 6)
44*2f2c4c7aSAndroid Build Coastguard Worker
45*2f2c4c7aSAndroid Build Coastguard Worker    param_list = itertools.product(PARAMS_A, PARAMS_B, PARAMS_C)
46*2f2c4c7aSAndroid Build Coastguard Worker    util.InjectParameterizedTest(cls, param_list, cls.NameGenerator)
47*2f2c4c7aSAndroid Build Coastguard Worker
48*2f2c4c7aSAndroid Build Coastguard Worker  def ParamTestDummyFunc(self, a, b, c):
49*2f2c4c7aSAndroid Build Coastguard Worker    self.tests_run_list.append(
50*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_" + ParmeterizationTest.NameGenerator(a, b, c))
51*2f2c4c7aSAndroid Build Coastguard Worker
52*2f2c4c7aSAndroid Build Coastguard Worker  def testParameterization(self):
53*2f2c4c7aSAndroid Build Coastguard Worker    expected = [
54*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_1_3_5",
55*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_1_3_6",
56*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_1_4_5",
57*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_1_4_6",
58*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_2_3_5",
59*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_2_3_6",
60*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_2_4_5",
61*2f2c4c7aSAndroid Build Coastguard Worker        "testDummyFunc_2_4_6",
62*2f2c4c7aSAndroid Build Coastguard Worker    ]
63*2f2c4c7aSAndroid Build Coastguard Worker
64*2f2c4c7aSAndroid Build Coastguard Worker    actual = [name for name in dir(self) if name.startswith("testDummyFunc")]
65*2f2c4c7aSAndroid Build Coastguard Worker
66*2f2c4c7aSAndroid Build Coastguard Worker    # Check that name and contents are equal
67*2f2c4c7aSAndroid Build Coastguard Worker    self.assertEqual(len(expected), len(actual))
68*2f2c4c7aSAndroid Build Coastguard Worker    self.assertEqual(sorted(expected), sorted(actual))
69*2f2c4c7aSAndroid Build Coastguard Worker
70*2f2c4c7aSAndroid Build Coastguard Worker    # Start a clean list, and run all the tests.
71*2f2c4c7aSAndroid Build Coastguard Worker    self.tests_run_list = list()
72*2f2c4c7aSAndroid Build Coastguard Worker    for test_name in expected:
73*2f2c4c7aSAndroid Build Coastguard Worker      test_method = getattr(self, test_name)
74*2f2c4c7aSAndroid Build Coastguard Worker      test_method()
75*2f2c4c7aSAndroid Build Coastguard Worker
76*2f2c4c7aSAndroid Build Coastguard Worker    # Make sure all tests have been run with the correct parameters
77*2f2c4c7aSAndroid Build Coastguard Worker    for test_name in expected:
78*2f2c4c7aSAndroid Build Coastguard Worker      self.assertTrue(test_name in self.tests_run_list)
79*2f2c4c7aSAndroid Build Coastguard Worker
80*2f2c4c7aSAndroid Build Coastguard Worker
81*2f2c4c7aSAndroid Build Coastguard Workerif __name__ == "__main__":
82*2f2c4c7aSAndroid Build Coastguard Worker  ParmeterizationTest.InjectTests()
83*2f2c4c7aSAndroid Build Coastguard Worker  unittest.main()
84