model_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package model
  2. import (
  3. "math/rand"
  4. "testing"
  5. "github.com/smartystreets/goconvey/convey"
  6. "go-common/library/log"
  7. )
  8. func TestElecPrepUPRankShift(t *testing.T) {
  9. convey.Convey("shift\n", t, func() {
  10. rank := &RankElecPrepUPProto{
  11. Count: 2,
  12. UPMID: 1,
  13. Size_: 100,
  14. }
  15. rank.List = []*RankElecPrepElementProto{
  16. &RankElecPrepElementProto{
  17. MID: 46333,
  18. Rank: 1,
  19. Amount: 100,
  20. }, &RankElecPrepElementProto{
  21. MID: 35858,
  22. Rank: 2,
  23. Amount: 99,
  24. }, &RankElecPrepElementProto{
  25. MID: 233,
  26. Rank: 3,
  27. Amount: 98,
  28. }, &RankElecPrepElementProto{
  29. MID: 2,
  30. Rank: 4,
  31. Amount: 1,
  32. },
  33. }
  34. rank.shift(0, 2)
  35. log.Info("%s", rank)
  36. convey.So(rank.List, convey.ShouldHaveLength, 4)
  37. convey.So(rank.List[0], convey.ShouldBeNil)
  38. convey.So(rank.List[1].Amount, convey.ShouldEqual, 100)
  39. convey.So(rank.List[2].Amount, convey.ShouldEqual, 99)
  40. })
  41. }
  42. func TestElecPrepUPRankUpdate(t *testing.T) {
  43. convey.Convey("shuffle\n", t, func() {
  44. rank := &RankElecPrepUPProto{
  45. Count: 2,
  46. UPMID: 1,
  47. Size_: 100,
  48. }
  49. rank.List = []*RankElecPrepElementProto{
  50. &RankElecPrepElementProto{
  51. MID: 46333,
  52. Rank: 1,
  53. Amount: 100,
  54. }, &RankElecPrepElementProto{
  55. MID: 35858,
  56. Rank: 2,
  57. Amount: 99,
  58. }, &RankElecPrepElementProto{
  59. MID: 233,
  60. Rank: 3,
  61. Amount: 98,
  62. }, &RankElecPrepElementProto{
  63. MID: 2,
  64. Rank: 4,
  65. Amount: 1,
  66. },
  67. }
  68. rank.update(&RankElecPrepElementProto{
  69. MID: 2,
  70. Rank: 4,
  71. Amount: 2,
  72. })
  73. log.Info("%s", rank)
  74. })
  75. }
  76. func TestElecPrepUPRankInsert(t *testing.T) {
  77. convey.Convey("shuffle\n", t, func() {
  78. rank := &RankElecPrepUPProto{
  79. Count: 2,
  80. UPMID: 1,
  81. Size_: 100,
  82. }
  83. rank.List = []*RankElecPrepElementProto{
  84. &RankElecPrepElementProto{
  85. MID: 46333,
  86. Rank: 1,
  87. Amount: 100,
  88. }, &RankElecPrepElementProto{
  89. MID: 35858,
  90. Rank: 2,
  91. Amount: 99,
  92. }, &RankElecPrepElementProto{
  93. MID: 233,
  94. Rank: 3,
  95. Amount: 98,
  96. }, &RankElecPrepElementProto{
  97. MID: 2,
  98. Rank: 4,
  99. Amount: 1,
  100. },
  101. }
  102. rank.insert(&RankElecPrepElementProto{
  103. MID: 322,
  104. Rank: -1,
  105. Amount: 101,
  106. })
  107. log.Info("%s", rank)
  108. })
  109. }
  110. func TestElecPrepUPRankCharge(t *testing.T) {
  111. convey.Convey("charge\n", t, func() {
  112. rank := &RankElecPrepUPProto{
  113. Count: 0,
  114. UPMID: 2,
  115. Size_: 3,
  116. }
  117. rank.Charge(35858, 100, true)
  118. convey.So(len(rank.List), convey.ShouldEqual, 1)
  119. convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
  120. MID: 35858,
  121. Rank: 1,
  122. Amount: 100,
  123. })
  124. rank.Charge(46333, 100, true)
  125. convey.So(len(rank.List), convey.ShouldEqual, 2)
  126. convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
  127. MID: 46333,
  128. Rank: 2,
  129. Amount: 100,
  130. })
  131. rank.Charge(233, 100, true)
  132. convey.So(len(rank.List), convey.ShouldEqual, 3)
  133. convey.So(rank.List[2], convey.ShouldResemble, &RankElecPrepElementProto{
  134. MID: 233,
  135. Rank: 3,
  136. Amount: 100,
  137. })
  138. log.Info("%s", rank)
  139. rank.Charge(46333, 101, false)
  140. log.Info("%s", rank)
  141. convey.So(len(rank.List), convey.ShouldEqual, 3)
  142. convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
  143. MID: 46333,
  144. Rank: 1,
  145. Amount: 101,
  146. })
  147. convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
  148. MID: 35858,
  149. Rank: 2,
  150. Amount: 100,
  151. })
  152. rank.Charge(233, 200, false)
  153. convey.So(len(rank.List), convey.ShouldEqual, 3)
  154. convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
  155. MID: 233,
  156. Rank: 1,
  157. Amount: 200,
  158. })
  159. convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
  160. MID: 46333,
  161. Rank: 2,
  162. Amount: 101,
  163. })
  164. log.Info("%s", rank)
  165. })
  166. }
  167. func TestElecPrepUPRankUpdateMessage(t *testing.T) {
  168. convey.Convey("charge\n", t, func() {
  169. rank := &RankElecPrepUPProto{
  170. Count: 0,
  171. UPMID: 2,
  172. Size_: 3,
  173. }
  174. rank.Charge(35858, 100, true)
  175. rank.Charge(46333, 100, true)
  176. rank.Charge(233, 100, true)
  177. rank.UpdateMessage(35858, "ut", false)
  178. rank.UpdateMessage(46333, "ut", true)
  179. rank.UpdateMessage(233, "ut-hello", false)
  180. convey.So(rank.Find(35858), convey.ShouldNotBeNil)
  181. convey.So(rank.Find(35858).Message, convey.ShouldNotBeNil)
  182. convey.So(rank.Find(35858).Message.Message, convey.ShouldEqual, "ut")
  183. convey.So(rank.Find(35858).Message.Hidden, convey.ShouldEqual, false)
  184. })
  185. }
  186. func TestElecPrepUPRankChargeRandom(t *testing.T) {
  187. rank := &RankElecPrepUPProto{
  188. Count: 0,
  189. UPMID: 1,
  190. Size_: 100,
  191. }
  192. for i := 0; i < 100; i++ {
  193. rank.Charge(randomEle())
  194. }
  195. log.Info("%s", rank)
  196. }
  197. func BenchmarkElecPrepUPRankCharge(b *testing.B) {
  198. rank := &RankElecPrepUPProto{
  199. Count: 0,
  200. UPMID: 1,
  201. Size_: 100,
  202. }
  203. for i := 0; i < b.N; i++ {
  204. rank.Charge(randomEle())
  205. }
  206. log.Info("%s", rank)
  207. }
  208. var (
  209. mids = []int64{2, 46333, 35858, 233}
  210. i = 0
  211. )
  212. func randomEle() (mid int64, amount int64, isNew bool) {
  213. mid = mids[rand.Intn(len(mids))]
  214. i++
  215. amount = int64(i)
  216. isNew = true
  217. return
  218. }
  219. func TestElecUserSetting(t *testing.T) {
  220. convey.Convey("TestElecUserSetting", t, func() {
  221. set := ElecUserSetting(2147483646)
  222. convey.So(set.ShowMessage(), convey.ShouldBeFalse)
  223. set = ElecUserSetting(0x7ffffff)
  224. convey.So(set.ShowMessage(), convey.ShouldBeTrue)
  225. set = ElecUserSetting(0x1)
  226. convey.So(set.ShowMessage(), convey.ShouldBeTrue)
  227. })
  228. }