Exercism: Rust version of the 'Matching Brackets' exercise.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.0 KiB

3 years ago
  1. use matching_brackets::brackets_are_balanced;
  2. #[test]
  3. fn paired_square_brackets() {
  4. assert!(brackets_are_balanced("[]"));
  5. }
  6. #[test]
  7. //#[ignore]
  8. fn empty_string() {
  9. assert!(brackets_are_balanced(""));
  10. }
  11. #[test]
  12. //#[ignore]
  13. fn unpaired_brackets() {
  14. assert!(!brackets_are_balanced("[["));
  15. }
  16. #[test]
  17. //#[ignore]
  18. fn wrong_ordered_brackets() {
  19. assert!(!brackets_are_balanced("}{"));
  20. }
  21. #[test]
  22. //#[ignore]
  23. fn wrong_closing_bracket() {
  24. assert!(!brackets_are_balanced("{]"));
  25. }
  26. #[test]
  27. //#[ignore]
  28. fn paired_with_whitespace() {
  29. assert!(brackets_are_balanced("{ }"));
  30. }
  31. #[test]
  32. //#[ignore]
  33. fn partially_paired_brackets() {
  34. assert!(!brackets_are_balanced("{[])"));
  35. }
  36. #[test]
  37. //#[ignore]
  38. fn simple_nested_brackets() {
  39. assert!(brackets_are_balanced("{[]}"));
  40. }
  41. #[test]
  42. //#[ignore]
  43. fn several_paired_brackets() {
  44. assert!(brackets_are_balanced("{}[]"));
  45. }
  46. #[test]
  47. //#[ignore]
  48. fn paired_and_nested_brackets() {
  49. assert!(brackets_are_balanced("([{}({}[])])"));
  50. }
  51. #[test]
  52. //#[ignore]
  53. fn unopened_closing_brackets() {
  54. assert!(!brackets_are_balanced("{[)][]}"));
  55. }
  56. #[test]
  57. //#[ignore]
  58. fn unpaired_and_nested_brackets() {
  59. assert!(!brackets_are_balanced("([{])"));
  60. }
  61. #[test]
  62. //#[ignore]
  63. fn paired_and_wrong_nested_brackets() {
  64. assert!(!brackets_are_balanced("[({]})"));
  65. }
  66. #[test]
  67. //#[ignore]
  68. fn paired_and_incomplete_brackets() {
  69. assert!(!brackets_are_balanced("{}["));
  70. }
  71. #[test]
  72. //#[ignore]
  73. fn too_many_closing_brackets() {
  74. assert!(!brackets_are_balanced("[]]"));
  75. }
  76. #[test]
  77. //#[ignore]
  78. fn early_incomplete_brackets() {
  79. assert!(!brackets_are_balanced(")()"));
  80. }
  81. #[test]
  82. //#[ignore]
  83. fn early_mismatched_brackets() {
  84. assert!(!brackets_are_balanced("{)()"));
  85. }
  86. #[test]
  87. //#[ignore]
  88. fn math_expression() {
  89. assert!(brackets_are_balanced("(((185 + 223.85) * 15) - 543)/2"));
  90. }
  91. #[test]
  92. //#[ignore]
  93. fn complex_latex_expression() {
  94. let input = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \
  95. \\end{array}\\right)";
  96. assert!(brackets_are_balanced(input));
  97. }