2
0

strategy_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package sentio
  2. import (
  3. "reflect"
  4. "slices"
  5. "testing"
  6. )
  7. func TestCompareStrategyOrders(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. orders []StrategyOrder
  11. want []StrategyOrder
  12. }{
  13. {
  14. name: "Sell first",
  15. orders: []StrategyOrder{
  16. {
  17. Action: OrderBuy,
  18. Ratio: .3,
  19. },
  20. {
  21. Action: OrderSell,
  22. Ratio: 1,
  23. },
  24. },
  25. want: []StrategyOrder{
  26. {
  27. Action: OrderSell,
  28. Ratio: 1,
  29. },
  30. {
  31. Action: OrderBuy,
  32. Ratio: .3,
  33. },
  34. },
  35. },
  36. {
  37. name: "Buy largest first",
  38. orders: []StrategyOrder{
  39. {
  40. Action: OrderBuy,
  41. Ratio: .3,
  42. },
  43. {
  44. Action: OrderBuy,
  45. Ratio: .6,
  46. },
  47. },
  48. want: []StrategyOrder{
  49. {
  50. Action: OrderBuy,
  51. Ratio: .6,
  52. },
  53. {
  54. Action: OrderBuy,
  55. Ratio: .3,
  56. },
  57. },
  58. },
  59. {
  60. name: "Sell largest first",
  61. orders: []StrategyOrder{
  62. {
  63. Action: OrderSell,
  64. Ratio: .3,
  65. },
  66. {
  67. Action: OrderSell,
  68. Ratio: .6,
  69. },
  70. },
  71. want: []StrategyOrder{
  72. {
  73. Action: OrderSell,
  74. Ratio: .6,
  75. },
  76. {
  77. Action: OrderSell,
  78. Ratio: .3,
  79. },
  80. },
  81. },
  82. {
  83. name: "Sell largest first then buy",
  84. orders: []StrategyOrder{
  85. {
  86. Action: OrderSell,
  87. Ratio: .3,
  88. },
  89. {
  90. Action: OrderBuy,
  91. Ratio: .6,
  92. },
  93. {
  94. Action: OrderSell,
  95. Ratio: .6,
  96. },
  97. },
  98. want: []StrategyOrder{
  99. {
  100. Action: OrderSell,
  101. Ratio: .6,
  102. },
  103. {
  104. Action: OrderSell,
  105. Ratio: .3,
  106. },
  107. {
  108. Action: OrderBuy,
  109. Ratio: .6,
  110. },
  111. },
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. slices.SortFunc(tt.orders, CompareStrategyOrders)
  117. if ok := reflect.DeepEqual(tt.want, tt.orders); !ok {
  118. t.Errorf("SortFunc = %v, want %v", tt.orders, tt.want)
  119. }
  120. })
  121. }
  122. }