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.
 
 
 

49 lines
1.1 KiB

  1. package common
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. var (
  7. testCase = map[uint32][4]byte {
  8. 0: [...]byte{0, 0, 0, 0},
  9. 1: [...]byte{0, 0, 0, 1},
  10. 256: [...]byte{0, 0, 1, 0},
  11. 256 * 256: [...]byte{0, 1, 0, 0},
  12. 256 * 256 * 256: [...]byte{1, 0, 0, 0},
  13. 256 * 256 * 256 + 256 * 256 + 256 + 1: [...]byte{1, 1, 1, 1},
  14. 4294967295 : [...]byte{0xFF, 0xFF, 0xFF, 0xFF},
  15. }
  16. )
  17. func TestUint32ToBytes(t *testing.T) {
  18. for k, v := range testCase {
  19. b := Uint32ToBytes(k)
  20. if bytes.Compare(b[:], v[:]) != 0 {
  21. t.Errorf("%v was expected, but %v was got", v, b)
  22. }
  23. }
  24. }
  25. func TestBytesToUint32s(t *testing.T) {
  26. for k, v := range testCase {
  27. u := BytesToUint32([4]byte(v))
  28. if u != k {
  29. t.Errorf("%v was expected, but %v was got", k, u)
  30. }
  31. }
  32. }
  33. func BenchmarkByteToUnit32(b * testing.B) {
  34. for i := 0; i < b.N; i++ {
  35. BytesToUint32([4]byte{0xF, 0xF, 0xF, 0xF});
  36. }
  37. }
  38. func BenchmarkUint32ToByte(b *testing.B) {
  39. for i := 0; i < b.N; i++ {
  40. Uint32ToBytes(123456);
  41. }
  42. }