xref: /aosp_15_r20/external/toolchain-utils/binary_search_tool/test/gen_obj.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Script to generate a list of object files.
8
90 represents a good object file.
101 represents a bad object file.
11"""
12
13
14import argparse
15import os
16import random
17import sys
18
19from binary_search_tool.test import common
20
21
22def Main(argv):
23    """Generates a list, the value of each element is 0 or 1.
24
25    The number of 1s in the list is specified by bad_obj_num.
26    The others are all 0s. The total number of 0s and 1s is specified by obj_num.
27
28    Args:
29      argv: argument from command line
30
31    Returns:
32      0 always.
33    """
34    parser = argparse.ArgumentParser()
35    parser.add_argument(
36        "-n",
37        "--obj_num",
38        dest="obj_num",
39        default=common.DEFAULT_OBJECT_NUMBER,
40        help=("Number of total objects."),
41    )
42    parser.add_argument(
43        "-b",
44        "--bad_obj_num",
45        dest="bad_obj_num",
46        default=common.DEFAULT_BAD_OBJECT_NUMBER,
47        help=(
48            "Number of bad objects. Must be great than or "
49            "equal to zero and less than total object "
50            "number."
51        ),
52    )
53    parser.add_argument(
54        "-o",
55        "--obj_list",
56        dest="obj_list",
57        default="",
58        help=(
59            "List of comma seperated objects to generate. "
60            "A 0 means the object is good, a 1 means the "
61            "object is bad."
62        ),
63    )
64    options = parser.parse_args(argv)
65
66    obj_num = int(options.obj_num)
67    bad_obj_num = int(options.bad_obj_num)
68    bad_to_gen = int(options.bad_obj_num)
69    obj_list = options.obj_list
70    if not obj_list:
71        obj_list = []
72        for i in range(obj_num):
73            if bad_to_gen > 0 and random.randint(1, obj_num) <= bad_obj_num:
74                obj_list.append(1)
75                bad_to_gen -= 1
76            else:
77                obj_list.append(0)
78        while bad_to_gen > 0:
79            t = random.randint(0, obj_num - 1)
80            if obj_list[t] == 0:
81                obj_list[t] = 1
82                bad_to_gen -= 1
83    else:
84        obj_list = obj_list.split(",")
85
86    if os.path.isfile(common.OBJECTS_FILE):
87        os.remove(common.OBJECTS_FILE)
88    if os.path.isfile(common.WORKING_SET_FILE):
89        os.remove(common.WORKING_SET_FILE)
90
91    with open(common.OBJECTS_FILE, "w", encoding="utf-8") as f:
92        with open(common.WORKING_SET_FILE, "w", encoding="utf-8") as w:
93            for i in obj_list:
94                f.write("{0}\n".format(i))
95                w.write("{0}\n".format(i))
96
97    obj_num = len(obj_list)
98    bad_obj_num = obj_list.count(1)
99    print(
100        "Generated {0} object files, with {1} bad ones.".format(
101            obj_num, bad_obj_num
102        )
103    )
104
105    return 0
106
107
108if __name__ == "__main__":
109    retval = Main(sys.argv[1:])
110    sys.exit(retval)
111