Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
package gameServerMgr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"goutil/timeUtil"
|
||||
"goutil/typeUtil"
|
||||
)
|
||||
|
||||
func TestActiveServer(t *testing.T) {
|
||||
var managecenterUrl = "https://managecenterapitest-ds3.7qule.com/"
|
||||
var serverGroupId int32 = 1000
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetServerGroup(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region测试获取服务器组
|
||||
serverGroup := GetServerGroup()
|
||||
if serverGroup == nil {
|
||||
t.Fatalf("获取服务器组[GetServerGroup]失败:serverGroup = nil")
|
||||
}
|
||||
|
||||
if serverGroup.Id != serverGroupId {
|
||||
t.Fatalf("获取服务器组[GetServerGroup]失败,期望[%d],结果[%d]", serverGroupId, serverGroup.Id)
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestGetDbConfig(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region测试获取服务器组
|
||||
serverGroup := GetServerGroup()
|
||||
if serverGroup == nil {
|
||||
t.Fatalf("获取服务器组[GetServerGroup]失败:serverGroup = nil")
|
||||
}
|
||||
|
||||
if serverGroup.Id != serverGroupId {
|
||||
t.Fatalf("获取服务器组[GetServerGroup]失败,期望[%d],结果[%d]", serverGroupId, serverGroup.Id)
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region 测试获取数据库配置
|
||||
/*
|
||||
{
|
||||
"GameDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_player_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;",
|
||||
"LogDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_log_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;",
|
||||
"GameModelDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_model_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=10;maximumpoolsize=10;command timeout=60;"
|
||||
}
|
||||
*/
|
||||
gameDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_player_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;"
|
||||
logDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_log_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;"
|
||||
modelDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_model_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=10;maximumpoolsize=10;command timeout=60;"
|
||||
|
||||
dbConfig, err := serverGroup.GetDBConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("获取服务器数据库配置[GetDBConfig]失败:%v", error)
|
||||
}
|
||||
|
||||
if dbConfig.GameDB != gameDb {
|
||||
t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,GameDB,期望[%s],结果[%s]", gameDb, dbConfig.GameDB)
|
||||
}
|
||||
|
||||
if dbConfig.LogDB != logDb {
|
||||
t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,LogDB,期望[%s],结果[%s]", logDb, dbConfig.LogDB)
|
||||
}
|
||||
|
||||
if dbConfig.GameModelDB != modelDb {
|
||||
t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,GameModelDB,期望[%s],结果[%s]", modelDb, dbConfig.GameModelDB)
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestGetServerItem(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
var serverId = serverGroupId
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region测试获取服务器
|
||||
serverItem, exists := GetServerItem(partnerId, serverId)
|
||||
if !exists || serverItem == nil {
|
||||
t.Fatalf("获取服务器[GetServerItem][%d]失败,PartnerId[%d],服务器不存在!", partnerId, serverId)
|
||||
}
|
||||
|
||||
if serverItem.Id != serverId {
|
||||
t.Fatalf("获取服务器[GetServerItem]失败,期望[%d],结果[%d]", serverId, serverItem.Id)
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestGetServerName(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
var serverId = serverGroupId
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试获取服务器名称
|
||||
var expectServerName = "开发测试服"
|
||||
serverName := GetServerName(partnerId, serverId)
|
||||
if serverName != expectServerName {
|
||||
t.Fatalf("获取服务器名称[GetServerName]失败,期望[%s],结果[%s]", expectServerName, serverName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfServerExists(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
var serverId = serverGroupId
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试服务器是否存在
|
||||
if !IfServerExists(partnerId, serverId) {
|
||||
t.Fatalf("判断服务器是否存在[IfServerExists]失败,PartnerId[%d],服务器[%d]不存在", partnerId, serverId)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPartnerServerPairString(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//获取合作商、服务器的组合字符串
|
||||
expectStr := "1001_20001|6666001_20001|"
|
||||
partnerServerPairStr := GetPartnerServerPairString()
|
||||
if expectStr != partnerServerPairStr {
|
||||
t.Fatalf("获取合作商、服务器的组合字符串[GetPartnerServerPairString]失败,期望[%s],结果[%s]", expectStr, partnerServerPairStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLoginKey(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region测试获取登录密钥
|
||||
var expectStr = "a0482eaf-14e8-4a65-950e-864214f62da5"
|
||||
loginKey, exists := GetLoginKey(partnerId)
|
||||
if !exists {
|
||||
t.Fatalf("获取登录密钥[GetLoginKey]失败,未找到渠道[%d]配置", partnerId)
|
||||
}
|
||||
|
||||
if expectStr != loginKey {
|
||||
t.Fatalf("获取登录密钥[GetLoginKey]失败,期望[%s],结果[%s]", expectStr, loginKey)
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestGetServerOpenDate(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试获取开服日期 2020/4/21 20:19:56
|
||||
expectOpenDate := time.Date(2020, 4, 21, 20, 19, 56, 0, time.Local)
|
||||
openTimeTick := GetServerOpenDate()
|
||||
openDate, _ := typeUtil.DateTime(openTimeTick)
|
||||
if openDate != expectOpenDate {
|
||||
t.Fatalf("获取服务器开服日期[GetServerOpenDate]失败,期望[%s],结果[%s]", timeUtil.ToDateTimeString(expectOpenDate), timeUtil.ToDateTimeString(openDate))
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerOpenDays(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试获取服务器开服天数
|
||||
now := time.Now()
|
||||
nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
|
||||
expectOpenDate := time.Date(2020, 4, 21, 0, 0, 0, 0, time.Local)
|
||||
expectOpenDays := int32(nowDate.Sub(expectOpenDate).Hours()/24) + 1
|
||||
openDays := ServerOpenDays()
|
||||
if expectOpenDays != openDays {
|
||||
t.Fatalf("获取服务器开服天数[ServerOpenDays]失败,期望[%d],结果[%d]", expectOpenDays, openDays)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMaintainStatus(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试服务器维护检查
|
||||
expectMsg := "维护中"
|
||||
maintainMessage, isMaintaining := CheckMaintainStatus()
|
||||
if isMaintaining && expectMsg != maintainMessage {
|
||||
t.Fatalf("服务器维护检查[CheckMaintainStatus]失败,期望维护消息[%s],结果维护消息[%s]", expectMsg, maintainMessage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckNewGameVersion(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
var serverId = serverGroupId
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region 测试检查是否有新版本
|
||||
|
||||
var gameVersionId int32 = 55 //老版本
|
||||
var expectGameVersionUrl = "testUrl" //已配置的游戏版本地址(IOS用)
|
||||
gameVersionUrl, exist := CheckNewGameVersion(partnerId, serverId, gameVersionId)
|
||||
if !exist {
|
||||
t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,未找到渠道[%d]配置或检查版本[%d]失败", partnerId, gameVersionId)
|
||||
} else if gameVersionUrl != expectGameVersionUrl {
|
||||
t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,期望版本地址[%s],结果版本地址[%s]", expectGameVersionUrl, gameVersionUrl)
|
||||
}
|
||||
|
||||
gameVersionId = 100 //当前版本
|
||||
_, exist = CheckNewGameVersion(partnerId, serverId, gameVersionId)
|
||||
if exist {
|
||||
t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,渠道[%d],版本[%d]不应有更新", partnerId, gameVersionId)
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestCheckNewResourceVersion(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
var serverId = serverGroupId
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region 测试检查新资源版本
|
||||
partnerId = 6666001
|
||||
var gameVersionId int32 = 100
|
||||
resourceVersionName := "1587372891_9eab40313feec913cace0adf0fe05341" //非最新资源版本号
|
||||
_, exist := CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
if !exist {
|
||||
t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
}
|
||||
|
||||
gameVersionId = 100
|
||||
resourceVersionName = "1587374043_cee48a8611276d3e3450782a1585c1a3" //最新资源版本号
|
||||
_, exist = CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
if exist {
|
||||
t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],不应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
}
|
||||
|
||||
gameVersionId = 123456 //不存在的游戏版本
|
||||
resourceVersionName = "1587374043_cee48a8611276d3e3450782a1585c1a3"
|
||||
_, exist = CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
if exist {
|
||||
t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],不应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
||||
func TestIsInWhiteList(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//测试白名单
|
||||
partnerId = 1001
|
||||
var userId = "446bfd6ccccd4229aa295686f9e5855b" //已配置的白名单用户Id
|
||||
if !IsInWhiteList(partnerId, userId) {
|
||||
t.Fatalf("检查是否用户在白名单中[IsInWhiteList]失败,渠道[%d],UserId[%s]", partnerId, userId)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试获取服务器组
|
||||
func TestGetOtherConfigInfo(t *testing.T) {
|
||||
var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
|
||||
var partnerId int32 = 1001
|
||||
var serverGroupId int32 = 20001
|
||||
|
||||
//激活器服务器,获取mc配置
|
||||
error := ActiveServer(managecenterUrl, serverGroupId)
|
||||
if error != nil {
|
||||
t.Fatalf("激活服务器出现错误:%v", error)
|
||||
}
|
||||
|
||||
//#region测试获取其他配置
|
||||
var configKey = "AppKey"
|
||||
var expectConfigValue = "a0482eaf-14e8-4a65-950e-864214f62da5" //已配置的配置值
|
||||
configValue, exist, error := GetOtherConfigInfo(partnerId, configKey)
|
||||
if error != nil {
|
||||
t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,渠道[%d],ConfigKey[%s]", partnerId, configKey)
|
||||
}
|
||||
if !exist {
|
||||
t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,渠道[%d],ConfigKey[%s],渠道或配置不存在!", partnerId, configKey)
|
||||
}
|
||||
|
||||
if configValue != expectConfigValue {
|
||||
t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,期望[%s],结果[%s]", expectConfigValue, configValue)
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
package mathUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetRandRangeInt(t *testing.T) {
|
||||
lower, upper := 10, 100
|
||||
rand := GetRand().GetRandRangeInt(lower, upper)
|
||||
if rand < lower || rand >= upper {
|
||||
t.Errorf("Expected a num between %d and %d, but got %d", lower, upper, rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandInt(t *testing.T) {
|
||||
var n int = 100
|
||||
var rand int = GetRand().GetRandInt(n)
|
||||
if rand >= n {
|
||||
t.Errorf("Expected a num < %d, but got %d", n, rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandInt32(t *testing.T) {
|
||||
var n int32 = 100
|
||||
var rand int32 = GetRand().GetRandInt32(n)
|
||||
if rand >= n {
|
||||
t.Errorf("Expected a num < %d, but got %d", n, rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandInt64(t *testing.T) {
|
||||
var n int64 = 100
|
||||
var rand int64 = GetRand().GetRandInt64(n)
|
||||
if rand >= n {
|
||||
t.Errorf("Expected a num < %d, but got %d", n, rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandFloat32(t *testing.T) {
|
||||
var rand float32 = GetRand().GetRandFloat32()
|
||||
if rand >= 1 {
|
||||
t.Errorf("Expected a num < 1, but got %f", rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandFloat64(t *testing.T) {
|
||||
var rand float64 = GetRand().GetRandFloat64()
|
||||
if rand >= 1 {
|
||||
t.Errorf("Expected a num < 1, but got %f", rand)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandNumList(t *testing.T) {
|
||||
if _, err := GetRand().GetRandNumList(11, 10, 11, false); err.Error() != "minValue can't be bigger than maxValue." {
|
||||
t.Error("Expected err, but got nil")
|
||||
}
|
||||
|
||||
if _, err := GetRand().GetRandNumList(1, 10, 11, false); err.Error() != "随机的数量超过区间的元素数量" {
|
||||
t.Error("Expected err, but got nil")
|
||||
}
|
||||
|
||||
if _, err := GetRand().GetRandNumList(1, 10001, 10, false); err.Error() != "随机数的区间不能大于10000" {
|
||||
t.Error("Expected err, but got nil")
|
||||
}
|
||||
|
||||
randNumList, _ := GetRand().GetRandNumList(1, 10, 1, false)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
randNumList, _ = GetRand().GetRandNumList(1, 10, 3, false)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
randNumList, _ = GetRand().GetRandNumList(1, 10, 5, false)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
randNumList, _ = GetRand().GetRandNumList(1, 10, 7, false)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
randNumList, _ = GetRand().GetRandNumList(1, 10, 9, false)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
randNumList, _ = GetRand().GetRandNumList(1, 10, 11, true)
|
||||
fmt.Printf("randNumList:%v\n", randNumList)
|
||||
}
|
||||
|
||||
func TestGetRandIntList(t *testing.T) {
|
||||
source := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
|
||||
if _, err := GetRand().GetRandIntList(source, 11, false); err.Error() != "随机的数量超过列表的元素数量" {
|
||||
t.Error("Expected err, but got nil")
|
||||
}
|
||||
|
||||
randIntList, _ := GetRand().GetRandIntList(source, 1, false)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
randIntList, _ = GetRand().GetRandIntList(source, 3, false)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
randIntList, _ = GetRand().GetRandIntList(source, 5, false)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
randIntList, _ = GetRand().GetRandIntList(source, 7, false)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
randIntList, _ = GetRand().GetRandIntList(source, 9, false)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
randIntList, _ = GetRand().GetRandIntList(source, 10, true)
|
||||
fmt.Printf("randIntList:%v\n", randIntList)
|
||||
}
|
||||
|
||||
func TestGetRandInterfaceList(t *testing.T) {
|
||||
item1 := NewItem(1, "name1", 1)
|
||||
item2 := NewItem(2, "name1", 1)
|
||||
item3 := NewItem(3, "name1", 1)
|
||||
item4 := NewItem(4, "name1", 1)
|
||||
item5 := NewItem(5, "name1", 1)
|
||||
item6 := NewItem(6, "name1", 1)
|
||||
item7 := NewItem(7, "name1", 1)
|
||||
item8 := NewItem(8, "name1", 1)
|
||||
item9 := NewItem(9, "name1", 1)
|
||||
item10 := NewItem(10, "name1", 1)
|
||||
|
||||
source := make([]interface{}, 0, 10)
|
||||
source = append(source, item1)
|
||||
source = append(source, item2)
|
||||
source = append(source, item3)
|
||||
source = append(source, item4)
|
||||
source = append(source, item5)
|
||||
source = append(source, item6)
|
||||
source = append(source, item7)
|
||||
source = append(source, item8)
|
||||
source = append(source, item9)
|
||||
source = append(source, item10)
|
||||
|
||||
if _, err := GetRand().GetRandInterfaceList(source, 11, false); err.Error() != "随机的数量超过列表的元素数量" {
|
||||
t.Error("Expected err, but got nil")
|
||||
}
|
||||
|
||||
randInterfaceList, _ := GetRand().GetRandInterfaceList(source, 1, false)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
randInterfaceList, _ = GetRand().GetRandInterfaceList(source, 3, false)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
randInterfaceList, _ = GetRand().GetRandInterfaceList(source, 5, false)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
randInterfaceList, _ = GetRand().GetRandInterfaceList(source, 7, false)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
randInterfaceList, _ = GetRand().GetRandInterfaceList(source, 9, false)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
randInterfaceList, _ = GetRand().GetRandInterfaceList(source, 10, true)
|
||||
fmt.Printf("randInterfaceList:%v\n", randInterfaceList)
|
||||
}
|
||||
|
||||
func TestGetRandWeight(t *testing.T) {
|
||||
source := make([]IWeight, 0, 10)
|
||||
if _, err := GetRand().GetRandWeight(source); err == nil {
|
||||
t.Errorf("err should not be nil, but it's nil")
|
||||
}
|
||||
|
||||
item1 := NewItem(1, "name1", 1)
|
||||
item2 := NewItem(2, "name2", 2)
|
||||
item3 := NewItem(3, "name3", 3)
|
||||
item4 := NewItem(4, "name4", 4)
|
||||
item5 := NewItem(5, "name5", 5)
|
||||
item6 := NewItem(6, "name6", 60)
|
||||
item7 := NewItem(7, "name7", 70)
|
||||
item8 := NewItem(8, "name8", 80)
|
||||
item9 := NewItem(9, "name9", 90)
|
||||
item10 := NewItem(10, "name10", 100)
|
||||
|
||||
source = append(source, item1)
|
||||
source = append(source, item2)
|
||||
source = append(source, item3)
|
||||
source = append(source, item4)
|
||||
source = append(source, item5)
|
||||
source = append(source, item6)
|
||||
source = append(source, item7)
|
||||
source = append(source, item8)
|
||||
source = append(source, item9)
|
||||
source = append(source, item10)
|
||||
|
||||
data := make(map[int]int)
|
||||
for i := 0; i < 10000; i++ {
|
||||
if result, err := GetRand().GetRandWeight(source); err != nil {
|
||||
t.Errorf("err should be nil, but it's not:%s", err)
|
||||
} else {
|
||||
if item, ok := result.(*Item); !ok {
|
||||
t.Errorf("convert to Item failed")
|
||||
} else {
|
||||
if count, ok := data[item.Id]; ok {
|
||||
data[item.Id] = count + 1
|
||||
} else {
|
||||
data[item.Id] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
total := 0
|
||||
for _, v := range data {
|
||||
total += v
|
||||
}
|
||||
|
||||
k := 1
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 2
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 3
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 4
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 5
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 6
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 7
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 8
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 9
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
k = 10
|
||||
if v, ok := data[k]; ok {
|
||||
fmt.Printf("%d:%d, ratio:%d\n", k, v, v*100/total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandWeight2(t *testing.T) {
|
||||
source := make([]IWeight, 0, 10)
|
||||
if _, err := GetRand().GetRandWeight(source); err == nil {
|
||||
t.Errorf("err should not be nil, but it's nil")
|
||||
}
|
||||
|
||||
item1 := NewItem(1, "name1", 1)
|
||||
item2 := NewItem(2, "name2", 2)
|
||||
item3 := NewItem(3, "name3", 3)
|
||||
item4 := NewItem(4, "name4", 4)
|
||||
item5 := NewItem(5, "name5", 5)
|
||||
item6 := NewItem(6, "name6", 60)
|
||||
item7 := NewItem(7, "name7", 70)
|
||||
item8 := NewItem(8, "name8", 80)
|
||||
item9 := NewItem(9, "name9", 90)
|
||||
item10 := NewItem(10, "name10", 100)
|
||||
|
||||
source = append(source, item1)
|
||||
source = append(source, item2)
|
||||
source = append(source, item3)
|
||||
source = append(source, item4)
|
||||
source = append(source, item5)
|
||||
source = append(source, item6)
|
||||
source = append(source, item7)
|
||||
source = append(source, item8)
|
||||
source = append(source, item9)
|
||||
source = append(source, item10)
|
||||
|
||||
rindexs, err := GetRand().GetRandItemsByWeight(source, 2, false)
|
||||
if err != nil {
|
||||
t.Errorf("GetRandItemsByWeight出错:%s", err)
|
||||
}
|
||||
if len(rindexs) != 2 || rindexs[0] == rindexs[1] {
|
||||
t.Errorf("GetRandItemsByWeight随机内容出错")
|
||||
}
|
||||
|
||||
rindexs, err = GetRand().GetRandItemsByWeight(source, 10, false)
|
||||
if err != nil {
|
||||
t.Errorf("GetRandItemsByWeight出错:%s", err)
|
||||
}
|
||||
|
||||
if len(rindexs) != 10 {
|
||||
t.Errorf("GetRandItemsByWeight随机内容出错")
|
||||
}
|
||||
|
||||
_, err = GetRand().GetRandItemsByWeight(source, 12, false)
|
||||
if err == nil {
|
||||
t.Errorf("GetRandItemsByWeight出错:%s", err)
|
||||
}
|
||||
|
||||
rindexs, err = GetRand().GetRandItemsByWeight(source, 15, true)
|
||||
if err != nil {
|
||||
t.Errorf("GetRandItemsByWeight出错:%s", err)
|
||||
}
|
||||
if len(rindexs) != 15 {
|
||||
t.Errorf("GetRandItemsByWeight随机内容出错")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Id int
|
||||
Name string
|
||||
Weight int
|
||||
}
|
||||
|
||||
func (item *Item) GetWeight() int {
|
||||
return item.Weight
|
||||
}
|
||||
|
||||
func (item *Item) String() string {
|
||||
return fmt.Sprintf("Id:%d,Name:%s\n", item.Id, item.Name)
|
||||
}
|
||||
|
||||
func NewItem(id int, name string, weight int) *Item {
|
||||
return &Item{
|
||||
Id: id,
|
||||
Name: name,
|
||||
Weight: weight,
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GetRandom(t *testing.T) {
|
||||
rand, err := GetRand().GetRandNumList(1, 10000, 10, true)
|
||||
if err != nil {
|
||||
t.Log(fmt.Errorf("获取随机数错误:%s", err))
|
||||
}
|
||||
|
||||
t.Log(rand)
|
||||
}
|
||||
Reference in New Issue
Block a user