bitutil_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package bitutil
  10. import (
  11. "testing"
  12. )
  13. func TestCompareByteArray(t *testing.T) {
  14. testdata1 := []byte("Test")
  15. testdata2 := make([]byte, 4, 5)
  16. testdata3 := make([]byte, 3, 3)
  17. if CompareByteArray(testdata1, testdata2) {
  18. t.Error("Byte arrays should not be considered equal before copying data.")
  19. }
  20. if CompareByteArray(testdata1, testdata3) {
  21. t.Error("Byte arrays should not be considered equal if the length is different.")
  22. }
  23. copy(testdata2, testdata1)
  24. if cap(testdata1) == cap(testdata2) {
  25. t.Error("Capacity of testdata sclices should be different.")
  26. }
  27. if !CompareByteArray(testdata1, testdata2) {
  28. t.Error("Byte arrays should be considered equal.")
  29. }
  30. }
  31. func TestByteSizeString(t *testing.T) {
  32. // Test byte sizes
  33. testdata := []int64{10000, 1024, 500, 1233456, 44166037, 84166037, 5000000000}
  34. // non-ISU values
  35. expected1 := []string{"9.8 KiB", "1.0 KiB", "500 B", "1.2 MiB", "42.1 MiB", "80.3 MiB", "4.7 GiB"}
  36. // ISU values
  37. expected2 := []string{"10.0 kB", "1.0 kB", "500 B", "1.2 MB", "44.2 MB", "84.2 MB", "5.0 GB"}
  38. for i, test := range testdata {
  39. res := ByteSizeString(test, false)
  40. if res != expected1[i] {
  41. t.Error("Unexpected value for non-isu value:", test,
  42. "got:", res, "expected:", expected1[i])
  43. return
  44. }
  45. res = ByteSizeString(test, true)
  46. if res != expected2[i] {
  47. t.Error("Unexpected value for isu value:", test,
  48. "got:", res, "expected:", expected2[i])
  49. return
  50. }
  51. }
  52. }
  53. func TestHexDump(t *testing.T) {
  54. testdata := []byte("This is a test text. This is a test text.")
  55. res := HexDump(testdata)
  56. if res != "====\n"+
  57. "000000 54 68 69 73 20 69 73 20 61 20 This is a \n"+
  58. "00000a 74 65 73 74 20 74 65 78 74 2E test text.\n"+
  59. "000014 20 54 68 69 73 20 69 73 20 61 This is a\n"+
  60. "00001e 20 74 65 73 74 20 74 65 78 74 test text\n"+
  61. "000028 2E .\n"+
  62. "====\n" {
  63. t.Error("Invalid boundaries should cause an error")
  64. }
  65. }