common_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package common
  2. import (
  3. "bytes"
  4. "math/big"
  5. "os"
  6. "testing"
  7. checker "gopkg.in/check.v1"
  8. )
  9. type CommonSuite struct{}
  10. var _ = checker.Suite(&CommonSuite{})
  11. func (s *CommonSuite) TestOS(c *checker.C) {
  12. expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';')
  13. res := IsWindows()
  14. if !expwin {
  15. c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
  16. } else {
  17. c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
  18. }
  19. }
  20. func (s *CommonSuite) TestWindonziePath(c *checker.C) {
  21. iswindowspath := os.PathSeparator == '\\'
  22. path := "/opt/eth/test/file.ext"
  23. res := WindonizePath(path)
  24. ressep := string(res[0])
  25. if !iswindowspath {
  26. c.Assert(ressep, checker.Equals, "/")
  27. } else {
  28. c.Assert(ressep, checker.Not(checker.Equals), "/")
  29. }
  30. }
  31. func (s *CommonSuite) TestCommon(c *checker.C) {
  32. douglas := CurrencyToString(BigPow(10, 43))
  33. einstein := CurrencyToString(BigPow(10, 22))
  34. ether := CurrencyToString(BigPow(10, 19))
  35. finney := CurrencyToString(BigPow(10, 16))
  36. szabo := CurrencyToString(BigPow(10, 13))
  37. shannon := CurrencyToString(BigPow(10, 10))
  38. babbage := CurrencyToString(BigPow(10, 7))
  39. ada := CurrencyToString(BigPow(10, 4))
  40. wei := CurrencyToString(big.NewInt(10))
  41. c.Assert(douglas, checker.Equals, "10 Douglas")
  42. c.Assert(einstein, checker.Equals, "10 Einstein")
  43. c.Assert(ether, checker.Equals, "10 Ether")
  44. c.Assert(finney, checker.Equals, "10 Finney")
  45. c.Assert(szabo, checker.Equals, "10 Szabo")
  46. c.Assert(shannon, checker.Equals, "10 Shannon")
  47. c.Assert(babbage, checker.Equals, "10 Babbage")
  48. c.Assert(ada, checker.Equals, "10 Ada")
  49. c.Assert(wei, checker.Equals, "10 Wei")
  50. }
  51. func (s *CommonSuite) TestLarge(c *checker.C) {
  52. douglaslarge := CurrencyToString(BigPow(100000000, 43))
  53. adalarge := CurrencyToString(BigPow(100000000, 4))
  54. weilarge := CurrencyToString(big.NewInt(100000000))
  55. c.Assert(douglaslarge, checker.Equals, "10000E298 Douglas")
  56. c.Assert(adalarge, checker.Equals, "10000E7 Einstein")
  57. c.Assert(weilarge, checker.Equals, "100 Babbage")
  58. }
  59. //fromHex
  60. func TestFromHex(t *testing.T) {
  61. input := "0x01"
  62. expected := []byte{1}
  63. result := FromHex(input)
  64. if bytes.Compare(expected, result) != 0 {
  65. t.Errorf("Expected % x got % x", expected, result)
  66. }
  67. }
  68. func TestFromHexOddLength(t *testing.T) {
  69. input := "0x1"
  70. expected := []byte{1}
  71. result := FromHex(input)
  72. if bytes.Compare(expected, result) != 0 {
  73. t.Errorf("Expected % x got % x", expected, result)
  74. }
  75. }