best_shuffle.t 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!perl -T
  2. use utf8;
  3. use 5.006;
  4. use strict;
  5. use warnings;
  6. use Test::More;
  7. plan tests => 6;
  8. use Sidef;
  9. my $code = <<'EOT';
  10. func best_shuffle(str) {
  11. var s = str.chars
  12. var t = s.shuffle
  13. for i in ^s {
  14. for j in ^s {
  15. if ((i == j) || (t[i] == s[j]) || (t[j] == s[i])) {
  16. next
  17. }
  18. t[i, j] = t[j, i]
  19. break
  20. }
  21. }
  22. s ~Z== t -> count(true)
  23. }
  24. EOT
  25. my $sidef = Sidef->new(name => 'best_shuffle');
  26. my $bshuffle = $sidef->execute_code($code);
  27. my @tests = (
  28. {
  29. str => 'abracadabra',
  30. score => 0,
  31. },
  32. {
  33. str => 'seesaw',
  34. score => 0,
  35. },
  36. {
  37. str => 'elk',
  38. score => 0,
  39. },
  40. {
  41. str => 'grrrrrr',
  42. score => 5,
  43. },
  44. {
  45. str => 'up',
  46. score => 0,
  47. },
  48. {
  49. str => 'a',
  50. score => 1,
  51. },
  52. );
  53. foreach my $t (@tests) {
  54. my $score = $bshuffle->call(Sidef::Types::String::String->new($t->{str}));
  55. is("$score", "$t->{score}", "bshuffle($t->{str}) = $t->{score}");
  56. }