uuid_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package uuid_test
  2. import (
  3. "testing"
  4. . "github.com/v2ray/v2ray-core/common/uuid"
  5. v2testing "github.com/v2ray/v2ray-core/testing"
  6. "github.com/v2ray/v2ray-core/testing/assert"
  7. )
  8. func TestParseBytes(t *testing.T) {
  9. v2testing.Current(t)
  10. str := "2418d087-648d-4990-86e8-19dca1d006d3"
  11. bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
  12. uuid, err := ParseBytes(bytes)
  13. assert.Error(err).IsNil()
  14. assert.String(uuid).Equals(str)
  15. }
  16. func TestParseString(t *testing.T) {
  17. v2testing.Current(t)
  18. str := "2418d087-648d-4990-86e8-19dca1d006d3"
  19. expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
  20. uuid, err := ParseString(str)
  21. assert.Error(err).IsNil()
  22. assert.Bytes(uuid.Bytes()).Equals(expectedBytes)
  23. }
  24. func TestNewUUID(t *testing.T) {
  25. v2testing.Current(t)
  26. uuid := New()
  27. uuid2, err := ParseString(uuid.String())
  28. assert.Error(err).IsNil()
  29. assert.StringLiteral(uuid.String()).Equals(uuid2.String())
  30. assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes())
  31. }
  32. func TestRandom(t *testing.T) {
  33. v2testing.Current(t)
  34. uuid := New()
  35. uuid2 := New()
  36. assert.StringLiteral(uuid.String()).NotEquals(uuid2.String())
  37. assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes())
  38. }
  39. func TestEquals(t *testing.T) {
  40. v2testing.Current(t)
  41. var uuid *UUID = nil
  42. var uuid2 *UUID = nil
  43. assert.Bool(uuid.Equals(uuid2)).IsTrue()
  44. assert.Bool(uuid.Equals(New())).IsFalse()
  45. }
  46. func TestNext(t *testing.T) {
  47. v2testing.Current(t)
  48. uuid := New()
  49. uuid2 := uuid.Next()
  50. assert.Bool(uuid.Equals(uuid2)).IsFalse()
  51. }