1*795d594fSAndroid Build Coastguard Worker#!/usr/bin/python3 2*795d594fSAndroid Build Coastguard Worker# 3*795d594fSAndroid Build Coastguard Worker# Copyright (C) 2015 The Android Open Source Project 4*795d594fSAndroid Build Coastguard Worker# 5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*795d594fSAndroid Build Coastguard Worker# 9*795d594fSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*795d594fSAndroid Build Coastguard Worker# 11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*795d594fSAndroid Build Coastguard Worker# limitations under the License. 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker""" 18*795d594fSAndroid Build Coastguard WorkerCommon functions useful for writing test generators in python 19*795d594fSAndroid Build Coastguard Worker""" 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Workerimport itertools 22*795d594fSAndroid Build Coastguard Workerimport os 23*795d594fSAndroid Build Coastguard Workerimport string 24*795d594fSAndroid Build Coastguard Workerfrom pathlib import Path 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard WorkerBUILD_TOP = os.getenv("ANDROID_BUILD_TOP") 27*795d594fSAndroid Build Coastguard Workerif BUILD_TOP is None: 28*795d594fSAndroid Build Coastguard Worker print("ANDROID_BUILD_TOP not set. Please run build/envsetup.sh", file=sys.stderr) 29*795d594fSAndroid Build Coastguard Worker sys.exit(1) 30*795d594fSAndroid Build Coastguard Worker 31*795d594fSAndroid Build Coastguard Worker# An iterator which yields strings made from lowercase letters. First yields 32*795d594fSAndroid Build Coastguard Worker# all 1 length strings, then all 2 and so on. It does this alphabetically. 33*795d594fSAndroid Build Coastguard WorkerNAME_GEN = itertools.chain.from_iterable( 34*795d594fSAndroid Build Coastguard Worker map(lambda n: itertools.product(string.ascii_lowercase, repeat=n), 35*795d594fSAndroid Build Coastguard Worker itertools.count(1))) 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Workerdef gensym(): 38*795d594fSAndroid Build Coastguard Worker """ 39*795d594fSAndroid Build Coastguard Worker Returns a new, globally unique, identifier name that is a valid Java symbol 40*795d594fSAndroid Build Coastguard Worker on each call. 41*795d594fSAndroid Build Coastguard Worker """ 42*795d594fSAndroid Build Coastguard Worker return ''.join(next(NAME_GEN)) 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Workerdef filter_blanks(s): 45*795d594fSAndroid Build Coastguard Worker """ 46*795d594fSAndroid Build Coastguard Worker Takes a string returns the same string sans empty lines 47*795d594fSAndroid Build Coastguard Worker """ 48*795d594fSAndroid Build Coastguard Worker return "\n".join(a for a in s.split("\n") if a.strip() != "") 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Workerdef get_copyright(filetype = "java"): 51*795d594fSAndroid Build Coastguard Worker """ 52*795d594fSAndroid Build Coastguard Worker Returns the standard copyright header for the given filetype 53*795d594fSAndroid Build Coastguard Worker """ 54*795d594fSAndroid Build Coastguard Worker if filetype == "smali": 55*795d594fSAndroid Build Coastguard Worker return "\n".join(map(lambda a: "# " + a, get_copyright("java").split("\n"))) 56*795d594fSAndroid Build Coastguard Worker else: 57*795d594fSAndroid Build Coastguard Worker fname = filetype + ".txt" 58*795d594fSAndroid Build Coastguard Worker with (Path(BUILD_TOP)/"development"/"docs"/"copyright-templates"/fname).open() as template: 59*795d594fSAndroid Build Coastguard Worker return "".join(template.readlines()) 60*795d594fSAndroid Build Coastguard Worker 61*795d594fSAndroid Build Coastguard Workerdef subtree_sizes(n): 62*795d594fSAndroid Build Coastguard Worker """ 63*795d594fSAndroid Build Coastguard Worker A generator that yields a tuple containing a possible arrangement of subtree 64*795d594fSAndroid Build Coastguard Worker nodes for a tree with a total of 'n' leaf nodes. 65*795d594fSAndroid Build Coastguard Worker """ 66*795d594fSAndroid Build Coastguard Worker if n == 0: 67*795d594fSAndroid Build Coastguard Worker return 68*795d594fSAndroid Build Coastguard Worker elif n == 1: 69*795d594fSAndroid Build Coastguard Worker yield (0,) 70*795d594fSAndroid Build Coastguard Worker elif n == 2: 71*795d594fSAndroid Build Coastguard Worker yield (1, 1) 72*795d594fSAndroid Build Coastguard Worker else: 73*795d594fSAndroid Build Coastguard Worker for prevt in subtree_sizes(n - 1): 74*795d594fSAndroid Build Coastguard Worker prev = list(prevt) 75*795d594fSAndroid Build Coastguard Worker yield tuple([1] + prev) 76*795d594fSAndroid Build Coastguard Worker for i in range(len(prev)): 77*795d594fSAndroid Build Coastguard Worker prev[i] += 1 78*795d594fSAndroid Build Coastguard Worker yield tuple(prev) 79*795d594fSAndroid Build Coastguard Worker prev[i] -= 1 80*795d594fSAndroid Build Coastguard Worker 81