test_mawk.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import infra.basetest
  3. class TestMawk(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_MAWK=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def basic_mawk_tests(self):
  11. # Check the program can execute
  12. self.assertRunOk("mawk --version")
  13. # Check "mawk" can return a specific exit code
  14. code = 123
  15. cmd = "mawk 'BEGIN { exit(" + str(code) + "); }'"
  16. _, exit_code = self.emulator.run(cmd)
  17. self.assertEqual(exit_code, code)
  18. # Run a basic print program
  19. test_string = "Hello Buildroot"
  20. cmd = "mawk 'BEGIN {print \"" + test_string + "\"; }'"
  21. output, exit_code = self.emulator.run(cmd)
  22. self.assertEqual(exit_code, 0)
  23. self.assertEqual(output[0], test_string)
  24. def create_test_data(self):
  25. # Create some test data
  26. entries = ["one", "two", "three", "four"]
  27. for entry in entries:
  28. self.assertRunOk(f"echo {entry} >> data1.txt")
  29. def add_line_numbers(self):
  30. # Add line numbers with mawk
  31. cmd = "mawk '{ print NR \"\\t\" $1; }' data1.txt > data2.txt"
  32. self.assertRunOk(cmd)
  33. def sum_column(self):
  34. # Check the sum of the first column is 1+2+3+4 == 10
  35. awk_prg = "BEGIN { SUM = 0; } { SUM = SUM + $1; } END { print SUM; }"
  36. cmd = f"mawk '{awk_prg}' data2.txt"
  37. output, exit_code = self.emulator.run(cmd)
  38. self.assertEqual(exit_code, 0)
  39. self.assertEqual(int(output[0]), 10)
  40. def uppercase_column(self):
  41. # Extract only column 2 and convert it to upper case
  42. cmd = "mawk '{ print toupper($2); }' data2.txt > data3.txt"
  43. self.assertRunOk(cmd)
  44. # Prepare the same output using "data1.txt" and the "tr" command,
  45. # for verification
  46. cmd = "tr a-z A-Z < data1.txt > data3-tr.txt"
  47. self.assertRunOk(cmd)
  48. # "mawk" and "tr" output are expected to be the same
  49. self.assertRunOk("cmp data3.txt data3-tr.txt")
  50. def mawk_head(self):
  51. # Show the first 2 lines of a file
  52. cmd = "mawk 'NR <= 2 { print $0; }' data2.txt > data4.txt"
  53. self.assertRunOk(cmd)
  54. # Prepare the same output using the "head" command
  55. cmd = "head -2 data2.txt > data4-head.txt"
  56. self.assertRunOk(cmd)
  57. # "mawk" and "tr" output are expected to be the same
  58. self.assertRunOk("cmp data4.txt data4-head.txt")
  59. def mawk_specific(self):
  60. # Use the "-W dump" mawk specific option.
  61. # See: https://invisible-island.net/mawk/manpage/mawk.html
  62. # We create an arbitrary awk program with an integer and
  63. # string constant. We then check those constants are in the
  64. # mawk "assembler" output.
  65. awk_int = 12345
  66. awk_str = "Buildroot"
  67. awk_expr = f"print ($1 + {awk_int}) \"{awk_str}\";"
  68. awk_prg = "BEGIN { " + awk_expr + " }"
  69. cmd = f"mawk -W dump '{awk_prg}'"
  70. output, exit_code = self.emulator.run(cmd)
  71. self.assertEqual(exit_code, 0)
  72. out_str = "\n".join(output)
  73. self.assertIn(str(awk_int), out_str)
  74. self.assertIn(awk_str, out_str)
  75. def mawk_numeric(self):
  76. value = 1234
  77. squared_value = value * value
  78. cmd = "mawk 'BEGIN { print sqrt(" + str(squared_value) + "); }'"
  79. output, exit_code = self.emulator.run(cmd)
  80. self.assertEqual(exit_code, 0)
  81. self.assertEqual(int(output[0]), value)
  82. def test_run(self):
  83. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  84. self.emulator.boot(arch="armv5",
  85. kernel="builtin",
  86. options=["-initrd", cpio_file])
  87. self.emulator.login()
  88. self.basic_mawk_tests()
  89. self.create_test_data()
  90. self.add_line_numbers()
  91. self.sum_column()
  92. self.uppercase_column()
  93. self.mawk_head()
  94. self.mawk_specific()
  95. self.mawk_numeric()