dist_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package math
  17. import (
  18. "fmt"
  19. "math/big"
  20. "testing"
  21. )
  22. type summer struct {
  23. numbers []*big.Int
  24. }
  25. func (s summer) Len() int { return len(s.numbers) }
  26. func (s summer) Sum(i int) *big.Int {
  27. return s.numbers[i]
  28. }
  29. func TestSum(t *testing.T) {
  30. summer := summer{numbers: []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}}
  31. sum := Sum(summer)
  32. if sum.Cmp(big.NewInt(6)) != 0 {
  33. t.Errorf("got sum = %d, want 6", sum)
  34. }
  35. }
  36. func TestDist(t *testing.T) {
  37. var vectors = []Vector{
  38. Vector{big.NewInt(1000), big.NewInt(1234)},
  39. Vector{big.NewInt(500), big.NewInt(10023)},
  40. Vector{big.NewInt(1034), big.NewInt(1987)},
  41. Vector{big.NewInt(1034), big.NewInt(1987)},
  42. Vector{big.NewInt(8983), big.NewInt(1977)},
  43. Vector{big.NewInt(98382), big.NewInt(1887)},
  44. Vector{big.NewInt(12398), big.NewInt(1287)},
  45. Vector{big.NewInt(12398), big.NewInt(1487)},
  46. Vector{big.NewInt(12398), big.NewInt(1987)},
  47. Vector{big.NewInt(12398), big.NewInt(128)},
  48. Vector{big.NewInt(12398), big.NewInt(1987)},
  49. Vector{big.NewInt(1398), big.NewInt(187)},
  50. Vector{big.NewInt(12328), big.NewInt(1927)},
  51. Vector{big.NewInt(12398), big.NewInt(1987)},
  52. Vector{big.NewInt(22398), big.NewInt(1287)},
  53. Vector{big.NewInt(1370), big.NewInt(1981)},
  54. Vector{big.NewInt(12398), big.NewInt(1957)},
  55. Vector{big.NewInt(42198), big.NewInt(1987)},
  56. }
  57. VectorsBy(GasSort).Sort(vectors)
  58. fmt.Println(vectors)
  59. BP := big.NewInt(15)
  60. GL := big.NewInt(1000000)
  61. EP := big.NewInt(100)
  62. fmt.Println("BP", BP, "GL", GL, "EP", EP)
  63. GP := GasPrice(BP, GL, EP)
  64. fmt.Println("GP =", GP, "Wei per GU")
  65. S := len(vectors) / 4
  66. fmt.Println("L", len(vectors), "S", S)
  67. for i := 1; i <= S*4; i += S {
  68. fmt.Printf("T%d = %v\n", i, vectors[i])
  69. }
  70. g := VectorSum(GasSum).Sum(vectors)
  71. fmt.Printf("G = ∑g* (%v)\n", g)
  72. }