2
0

ech_combos.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def CombinationRepetitionUtil(chosen, arr, badarr, index,
  33. r, start, end):
  34. # Current combination is ready,
  35. # print it
  36. if index == r:
  37. # figure out if result should be good or bad and
  38. # print prefix, assuming $turl does support ECH so
  39. # should work if given "positive" parameters
  40. res = 1
  41. j = len(chosen) - 1
  42. while res and j >= 0:
  43. if chosen[j] in badarr:
  44. res = 0
  45. j = j - 1
  46. print("cli_test $turl 1", res, end = " ")
  47. # print combination but eliminating any runs of
  48. # two identical params
  49. for j in range(r):
  50. if j != 0 and chosen[j] != chosen[j-1]:
  51. print(chosen[j], end = " ")
  52. print()
  53. return
  54. # When no more elements are
  55. # there to put in chosen[]
  56. if start > n:
  57. return
  58. # Current is included, put
  59. # next at next location
  60. chosen[index] = arr[start]
  61. # Current is excluded, replace it
  62. # with next (Note that i+1 is passed,
  63. # but index is not changed)
  64. CombinationRepetitionUtil(chosen, arr, badarr, index + 1,
  65. r, start, end)
  66. CombinationRepetitionUtil(chosen, arr, badarr, index,
  67. r, start + 1, end)
  68. # The main function that prints all
  69. # combinations of size r in arr[] of
  70. # size n. This function mainly uses
  71. # CombinationRepetitionUtil()
  72. def CombinationRepetition(arr, badarr, n, r):
  73. # A temporary array to store
  74. # all combination one by one
  75. chosen = [0] * r
  76. # Print all combination using
  77. # temporary array 'chosen[]'
  78. CombinationRepetitionUtil(chosen, arr, badarr, 0, r, 0, n)
  79. # Driver code
  80. badarr = [ '--ech grease', '--ech false', '--ech ecl:$badecl', '--ech pn:$badpn' ]
  81. goodarr = [ '--ech hard', '--ech true', '--ech ecl:$goodecl', '--ech pn:$goodpn' ]
  82. arr = badarr + goodarr
  83. r = 8
  84. n = len(arr) - 1
  85. CombinationRepetition(arr, badarr, n, r)
  86. # This code is contributed by Vaibhav Kumar 12.