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.

118 lines
3.0 KiB

  1. package iconv
  2. import (
  3. "syscall"
  4. "testing"
  5. )
  6. type iconvTest struct {
  7. description string
  8. input string
  9. inputEncoding string
  10. output string
  11. outputEncoding string
  12. bytesRead int
  13. bytesWritten int
  14. err error
  15. }
  16. var iconvTests = []iconvTest{
  17. iconvTest{
  18. "simple utf-8 to latin1 conversion success",
  19. "Hello World!", "utf-8",
  20. "Hello World!", "latin1",
  21. 12, 12, nil,
  22. },
  23. iconvTest{
  24. "invalid source encoding causes EINVAL",
  25. "", "doesnotexist",
  26. "", "utf-8",
  27. 0, 0, syscall.EINVAL,
  28. },
  29. iconvTest{
  30. "invalid destination encoding causes EINVAL",
  31. "", "utf-8",
  32. "", "doesnotexist",
  33. 0, 0, syscall.EINVAL,
  34. },
  35. iconvTest{
  36. "invalid input sequence causes EILSEQ",
  37. "\xFF", "utf-8",
  38. "", "latin1",
  39. 0, 0, syscall.EILSEQ,
  40. },
  41. iconvTest{
  42. "invalid input causes partial output and EILSEQ",
  43. "Hello\xFF", "utf-8",
  44. "Hello", "latin1",
  45. 5, 5, syscall.EILSEQ,
  46. },
  47. }
  48. func TestConvertString(t *testing.T) {
  49. for _, test := range iconvTests {
  50. // perform the conversion
  51. output, err := ConvertString(test.input, test.inputEncoding, test.outputEncoding)
  52. // check that output and err match
  53. if output != test.output {
  54. t.Errorf("test \"%s\" failed, output did not match expected", test.description)
  55. }
  56. // check that err is same as expected
  57. if err != test.err {
  58. if test.err != nil {
  59. if err != nil {
  60. t.Errorf("test \"%s\" failed, got %s when expecting %s", test.description, err, test.err)
  61. } else {
  62. t.Errorf("test \"%s\" failed, got nil when expecting %s", test.description, test.err)
  63. }
  64. } else {
  65. t.Errorf("test \"%s\" failed, got unexpected error: %s", test.description, err)
  66. }
  67. }
  68. }
  69. }
  70. func TestConvert(t *testing.T) {
  71. for _, test := range iconvTests {
  72. // setup input buffer
  73. input := []byte(test.input)
  74. // setup a buffer as large as the expected bytesWritten
  75. output := make([]byte, 50)
  76. // peform the conversion
  77. bytesRead, bytesWritten, err := Convert(input, output, test.inputEncoding, test.outputEncoding)
  78. // check that bytesRead is same as expected
  79. if bytesRead != test.bytesRead {
  80. t.Errorf("test \"%s\" failed, bytesRead did not match expected", test.description)
  81. }
  82. // check that bytesWritten is same as expected
  83. if bytesWritten != test.bytesWritten {
  84. t.Errorf("test \"%s\" failed, bytesWritten did not match expected", test.description)
  85. }
  86. // check output bytes against expected - simplest to convert output to
  87. // string and then do an equality check which is actually a byte wise operation
  88. if string(output[:bytesWritten]) != test.output {
  89. t.Errorf("test \"%s\" failed, output did not match expected", test.description)
  90. }
  91. // check that err is same as expected
  92. if err != test.err {
  93. if test.err != nil {
  94. if err != nil {
  95. t.Errorf("test \"%s\" failed, got %s when expecting %s", test.description, err, test.err)
  96. } else {
  97. t.Errorf("test \"%s\" failed, got nil when expecting %s", test.description, test.err)
  98. }
  99. } else {
  100. t.Errorf("test \"%s\" failed, got unexpected error: %s", test.description, err)
  101. }
  102. }
  103. }
  104. }