123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- package model
- import (
- "math/rand"
- "testing"
- "github.com/smartystreets/goconvey/convey"
- "go-common/library/log"
- )
- func TestElecPrepUPRankShift(t *testing.T) {
- convey.Convey("shift\n", t, func() {
- rank := &RankElecPrepUPProto{
- Count: 2,
- UPMID: 1,
- Size_: 100,
- }
- rank.List = []*RankElecPrepElementProto{
- &RankElecPrepElementProto{
- MID: 46333,
- Rank: 1,
- Amount: 100,
- }, &RankElecPrepElementProto{
- MID: 35858,
- Rank: 2,
- Amount: 99,
- }, &RankElecPrepElementProto{
- MID: 233,
- Rank: 3,
- Amount: 98,
- }, &RankElecPrepElementProto{
- MID: 2,
- Rank: 4,
- Amount: 1,
- },
- }
- rank.shift(0, 2)
- log.Info("%s", rank)
- convey.So(rank.List, convey.ShouldHaveLength, 4)
- convey.So(rank.List[0], convey.ShouldBeNil)
- convey.So(rank.List[1].Amount, convey.ShouldEqual, 100)
- convey.So(rank.List[2].Amount, convey.ShouldEqual, 99)
- })
- }
- func TestElecPrepUPRankUpdate(t *testing.T) {
- convey.Convey("shuffle\n", t, func() {
- rank := &RankElecPrepUPProto{
- Count: 2,
- UPMID: 1,
- Size_: 100,
- }
- rank.List = []*RankElecPrepElementProto{
- &RankElecPrepElementProto{
- MID: 46333,
- Rank: 1,
- Amount: 100,
- }, &RankElecPrepElementProto{
- MID: 35858,
- Rank: 2,
- Amount: 99,
- }, &RankElecPrepElementProto{
- MID: 233,
- Rank: 3,
- Amount: 98,
- }, &RankElecPrepElementProto{
- MID: 2,
- Rank: 4,
- Amount: 1,
- },
- }
- rank.update(&RankElecPrepElementProto{
- MID: 2,
- Rank: 4,
- Amount: 2,
- })
- log.Info("%s", rank)
- })
- }
- func TestElecPrepUPRankInsert(t *testing.T) {
- convey.Convey("shuffle\n", t, func() {
- rank := &RankElecPrepUPProto{
- Count: 2,
- UPMID: 1,
- Size_: 100,
- }
- rank.List = []*RankElecPrepElementProto{
- &RankElecPrepElementProto{
- MID: 46333,
- Rank: 1,
- Amount: 100,
- }, &RankElecPrepElementProto{
- MID: 35858,
- Rank: 2,
- Amount: 99,
- }, &RankElecPrepElementProto{
- MID: 233,
- Rank: 3,
- Amount: 98,
- }, &RankElecPrepElementProto{
- MID: 2,
- Rank: 4,
- Amount: 1,
- },
- }
- rank.insert(&RankElecPrepElementProto{
- MID: 322,
- Rank: -1,
- Amount: 101,
- })
- log.Info("%s", rank)
- })
- }
- func TestElecPrepUPRankCharge(t *testing.T) {
- convey.Convey("charge\n", t, func() {
- rank := &RankElecPrepUPProto{
- Count: 0,
- UPMID: 2,
- Size_: 3,
- }
- rank.Charge(35858, 100, true)
- convey.So(len(rank.List), convey.ShouldEqual, 1)
- convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 35858,
- Rank: 1,
- Amount: 100,
- })
- rank.Charge(46333, 100, true)
- convey.So(len(rank.List), convey.ShouldEqual, 2)
- convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 46333,
- Rank: 2,
- Amount: 100,
- })
- rank.Charge(233, 100, true)
- convey.So(len(rank.List), convey.ShouldEqual, 3)
- convey.So(rank.List[2], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 233,
- Rank: 3,
- Amount: 100,
- })
- log.Info("%s", rank)
- rank.Charge(46333, 101, false)
- log.Info("%s", rank)
- convey.So(len(rank.List), convey.ShouldEqual, 3)
- convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 46333,
- Rank: 1,
- Amount: 101,
- })
- convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 35858,
- Rank: 2,
- Amount: 100,
- })
- rank.Charge(233, 200, false)
- convey.So(len(rank.List), convey.ShouldEqual, 3)
- convey.So(rank.List[0], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 233,
- Rank: 1,
- Amount: 200,
- })
- convey.So(rank.List[1], convey.ShouldResemble, &RankElecPrepElementProto{
- MID: 46333,
- Rank: 2,
- Amount: 101,
- })
- log.Info("%s", rank)
- })
- }
- func TestElecPrepUPRankUpdateMessage(t *testing.T) {
- convey.Convey("charge\n", t, func() {
- rank := &RankElecPrepUPProto{
- Count: 0,
- UPMID: 2,
- Size_: 3,
- }
- rank.Charge(35858, 100, true)
- rank.Charge(46333, 100, true)
- rank.Charge(233, 100, true)
- rank.UpdateMessage(35858, "ut", false)
- rank.UpdateMessage(46333, "ut", true)
- rank.UpdateMessage(233, "ut-hello", false)
- convey.So(rank.Find(35858), convey.ShouldNotBeNil)
- convey.So(rank.Find(35858).Message, convey.ShouldNotBeNil)
- convey.So(rank.Find(35858).Message.Message, convey.ShouldEqual, "ut")
- convey.So(rank.Find(35858).Message.Hidden, convey.ShouldEqual, false)
- })
- }
- func TestElecPrepUPRankChargeRandom(t *testing.T) {
- rank := &RankElecPrepUPProto{
- Count: 0,
- UPMID: 1,
- Size_: 100,
- }
- for i := 0; i < 100; i++ {
- rank.Charge(randomEle())
- }
- log.Info("%s", rank)
- }
- func BenchmarkElecPrepUPRankCharge(b *testing.B) {
- rank := &RankElecPrepUPProto{
- Count: 0,
- UPMID: 1,
- Size_: 100,
- }
- for i := 0; i < b.N; i++ {
- rank.Charge(randomEle())
- }
- log.Info("%s", rank)
- }
- var (
- mids = []int64{2, 46333, 35858, 233}
- i = 0
- )
- func randomEle() (mid int64, amount int64, isNew bool) {
- mid = mids[rand.Intn(len(mids))]
- i++
- amount = int64(i)
- isNew = true
- return
- }
- func TestElecUserSetting(t *testing.T) {
- convey.Convey("TestElecUserSetting", t, func() {
- set := ElecUserSetting(2147483646)
- convey.So(set.ShowMessage(), convey.ShouldBeFalse)
- set = ElecUserSetting(0x7ffffff)
- convey.So(set.ShowMessage(), convey.ShouldBeTrue)
- set = ElecUserSetting(0x1)
- convey.So(set.ShowMessage(), convey.ShouldBeTrue)
- })
- }
|