xref: /curl/tests/ech_combos.py (revision f81f6020)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#***************************************************************************
4#                                  _   _ ____  _
5#  Project                     ___| | | |  _ \| |
6#                             / __| | | | |_) | |
7#                            | (__| |_| |  _ <| |___
8#                             \___|\___/|_| \_\_____|
9#
10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11#
12# This software is licensed as described in the file COPYING, which
13# you should have received as part of this distribution. The terms
14# are also available at https://curl.se/docs/copyright.html.
15#
16# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17# copies of the Software, and permit persons to whom the Software is
18# furnished to do so, under the terms of the COPYING file.
19#
20# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21# KIND, either express or implied.
22#
23# SPDX-License-Identifier: curl
24#
25###########################################################################
26#
27# Python3 program to print all combination of size r in an array of size n.
28# This is used to generate test lines in tests/ech_test.sh.
29# This will be discarded in the process of moving from experimental,
30# but is worth preserving for the moment in case of changes to the
31# ECH command line args
32
33def CombinationRepetitionUtil(chosen, arr, badarr, index,
34                            r, start, end):
35
36    # Current combination is ready,
37    # print it
38    if index == r:
39        # figure out if result should be good or bad and
40        # print prefix, assuming $turl does support ECH so
41        # should work if given "positive" parameters
42        res = 1
43        j = len(chosen) - 1
44        while res and j >= 0:
45            if chosen[j] in badarr:
46                res = 0
47            j = j - 1
48        print("cli_test $turl 1", res, end = " ")
49        # print combination but eliminating any runs of
50        # two identical params
51        for j in range(r):
52            if j != 0 and chosen[j] != chosen[j-1]:
53                print(chosen[j], end = " ")
54
55        print()
56        return
57
58    # When no more elements are
59    # there to put in chosen[]
60    if start > n:
61        return
62
63    # Current is included, put
64    # next at next location
65    chosen[index] = arr[start]
66
67    # Current is excluded, replace it
68    # with next (Note that i+1 is passed,
69    # but index is not changed)
70    CombinationRepetitionUtil(chosen, arr, badarr, index + 1,
71                            r, start, end)
72    CombinationRepetitionUtil(chosen, arr, badarr, index,
73                            r, start + 1, end)
74
75# The main function that prints all
76# combinations of size r in arr[] of
77# size n. This function mainly uses
78# CombinationRepetitionUtil()
79def CombinationRepetition(arr, badarr, n, r):
80
81    # A temporary array to store
82    # all combination one by one
83    chosen = [0] * r
84
85    # Print all combination using
86    # temporary array 'chosen[]'
87    CombinationRepetitionUtil(chosen, arr, badarr, 0, r, 0, n)
88
89# Driver code
90badarr = [ '--ech grease', '--ech false', '--ech ecl:$badecl', '--ech pn:$badpn' ]
91goodarr = [ '--ech hard', '--ech true', '--ech ecl:$goodecl',  '--ech pn:$goodpn' ]
92arr = badarr + goodarr
93r = 8
94n = len(arr) - 1
95
96CombinationRepetition(arr, badarr, n, r)
97
98# This code is contributed by Vaibhav Kumar 12.
99