word-stat.test.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. require_once __DIR__ . '/word-stat.php';
  3. class TestSuite
  4. {
  5. private array $test_cases = array();
  6. public function it(string $descr, callable $fn)
  7. {
  8. $this->test_cases[$descr] = $fn;
  9. return $this;
  10. }
  11. public function run()
  12. {
  13. $errors = array();
  14. foreach ($this->test_cases as $descr => $test_case) {
  15. echo $descr . " ";
  16. try {
  17. $test_case();
  18. echo ".";
  19. } catch (Exception $e) {
  20. if (isset($errors[$descr])) {
  21. array_push($errors[$descr], $e);
  22. } else {
  23. $errors[$descr] = array($e);
  24. }
  25. echo "F";
  26. } finally {
  27. echo "\n";
  28. }
  29. }
  30. echo count($this->test_cases) . " cases, " . count($errors) . " Failures:\n";
  31. foreach ($errors as $descr => $errors) {
  32. echo $descr . "\n";
  33. foreach ($errors as $e) {
  34. echo $e . "\n";
  35. }
  36. echo "\n";
  37. }
  38. }
  39. public static function strictEqual($actual, $expect)
  40. {
  41. if ($actual !== $expect) {
  42. throw new Exception("actual: $actual, expect: $expect");
  43. }
  44. return static::class;
  45. }
  46. }
  47. $suite = new TestSuite();
  48. $suite->it("word_stat('') == 0", function () {
  49. assert(word_stat('') == 0);
  50. })
  51. ->it("将连续数字看作一个单词", function () {
  52. TestSuite::strictEqual(word_stat('1234567890 1234567890'), 2);
  53. })
  54. ->it("连续字母视为一个单词", function () {
  55. TestSuite::strictEqual(word_stat('Hello World'), 2)
  56. ::strictEqual(word_stat('Hello World'), 2)
  57. ::strictEqual(word_stat('Hello World '), 2)
  58. ::strictEqual(word_stat(' Hello World'), 2)
  59. ::strictEqual(word_stat('Hello,World'), 2);
  60. })
  61. ->it("数字与字母混合视为一个单词", function () {
  62. TestSuite::strictEqual(word_stat(("i18n")), 1);
  63. })
  64. ->it("每个汉字视为一个单词", function () {
  65. TestSuite::strictEqual(word_stat('你好,世界'), 4)
  66. ::strictEqual(word_stat('你好,世界'), 4)
  67. ::strictEqual(word_stat('〡〢〣'), 3)
  68. ::strictEqual(word_stat('𰀀'), 1); // CJK Extension G (Unicode 13)
  69. })
  70. ->it("每个假名视为一个单词", function () {
  71. TestSuite::strictEqual(word_stat('こんにちは'), 5)
  72. ::strictEqual(word_stat('カタナ'), 3);
  73. })
  74. ->it("每个谚文视为一个单词", function () {
  75. TestSuite::strictEqual(word_stat('안녕하세요'), 5);
  76. })
  77. ->it("忽略Emoji", function () {
  78. TestSuite::strictEqual(word_stat('👋🌍'), 0);
  79. })
  80. ->it("混合计算", function () {
  81. TestSuite::strictEqual(word_stat('Hello こんにちは 你好 1234567890'), 9);
  82. })
  83. ->run();