test_gawk.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import os
  2. import infra.basetest
  3. class TestGawk(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
  7. BR2_PACKAGE_GAWK=y
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def basic_gawk_tests(self):
  12. # Check the program can execute
  13. self.assertRunOk("gawk --version")
  14. # Check "awk" is "gawk": the Buildroot gawk package recipe is
  15. # supposed to install the symbolic link.
  16. output, exit_code = self.emulator.run("awk --version")
  17. self.assertEqual(exit_code, 0)
  18. self.assertTrue(output[0].startswith("GNU Awk"))
  19. # Check "gawk" can return a specific exit code
  20. code = 123
  21. cmd = "gawk 'BEGIN { exit(" + str(code) + "); }'"
  22. _, exit_code = self.emulator.run(cmd)
  23. self.assertEqual(exit_code, code)
  24. # Run a basic print program
  25. test_string = "Hello Buildroot"
  26. cmd = "gawk 'BEGIN {print \"" + test_string + "\"; }'"
  27. output, exit_code = self.emulator.run(cmd)
  28. self.assertEqual(exit_code, 0)
  29. self.assertEqual(output[0], test_string)
  30. def create_test_data(self):
  31. # Create some test data
  32. entries = ["one", "two", "three", "four"]
  33. for entry in entries:
  34. self.assertRunOk(f"echo {entry} >> data1.txt")
  35. def add_line_numbers(self):
  36. # Add line numbers with gawk
  37. cmd = "gawk '{ print NR \"\\t\" $1; }' data1.txt > data2.txt"
  38. self.assertRunOk(cmd)
  39. def sum_column(self):
  40. # Check the sum of the first column is 1+2+3+4 == 10
  41. awk_prg = "BEGIN { SUM = 0; } { SUM = SUM + $1; } END { print SUM; }"
  42. cmd = f"gawk '{awk_prg}' data2.txt"
  43. output, exit_code = self.emulator.run(cmd)
  44. self.assertEqual(exit_code, 0)
  45. self.assertEqual(int(output[0]), 10)
  46. def uppercase_column(self):
  47. # Extract only column 2 and convert it to upper case
  48. cmd = "gawk '{ print toupper($2); }' data2.txt > data3.txt"
  49. self.assertRunOk(cmd)
  50. # Prepare the same output using "data1.txt" and the "tr" command,
  51. # for verification
  52. cmd = "tr a-z A-Z < data1.txt > data3-tr.txt"
  53. self.assertRunOk(cmd)
  54. # "gawk" and "tr" output are expected to be the same
  55. self.assertRunOk("cmp data3.txt data3-tr.txt")
  56. def gawk_head(self):
  57. # Show the first 2 lines of a file
  58. cmd = "gawk 'NR <= 2 { print $0; }' data2.txt > data4.txt"
  59. self.assertRunOk(cmd)
  60. # Prepare the same output using the "head" command
  61. cmd = "head -2 data2.txt > data4-head.txt"
  62. self.assertRunOk(cmd)
  63. # "gawk" and "tr" output are expected to be the same
  64. self.assertRunOk("cmp data4.txt data4-head.txt")
  65. def gawk_specific(self):
  66. # Use PROCINFO, which is a gawk specific feature:
  67. # https://www.gnu.org/software/gawk/manual/gawk.html#POSIX_002fGNU
  68. awk_platform_prog = "BEGIN { print PROCINFO[\"platform\"]; }"
  69. cmd = f"gawk '{awk_platform_prog}'"
  70. output, exit_code = self.emulator.run(cmd)
  71. self.assertEqual(exit_code, 0)
  72. self.assertEqual(output[0], "posix")
  73. # Using the same gawk feature when running in POSIX mode should not
  74. # produce output.
  75. cmd = f"gawk --posix '{awk_platform_prog}'"
  76. output, exit_code = self.emulator.run(cmd)
  77. self.assertEqual(exit_code, 0)
  78. self.assertTrue(len(output) == 1 and len(output[0]) == 0)
  79. def gawk_numeric(self):
  80. value = 1234
  81. squared_value = value * value
  82. cmd = "gawk 'BEGIN { print sqrt(" + str(squared_value) + "); }'"
  83. output, exit_code = self.emulator.run(cmd)
  84. self.assertEqual(exit_code, 0)
  85. self.assertEqual(int(output[0]), value)
  86. def test_run(self):
  87. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  88. self.emulator.boot(arch="armv5",
  89. kernel="builtin",
  90. options=["-initrd", cpio_file])
  91. self.emulator.login()
  92. self.basic_gawk_tests()
  93. self.create_test_data()
  94. self.add_line_numbers()
  95. self.sum_column()
  96. self.uppercase_column()
  97. self.gawk_head()
  98. self.gawk_specific()
  99. self.gawk_numeric()