|
|
@@ -1,78 +0,0 @@
|
|
|
-package main
|
|
|
-
|
|
|
-import (
|
|
|
- "fmt"
|
|
|
- "strconv"
|
|
|
- "time"
|
|
|
-)
|
|
|
-
|
|
|
-// 分数计算常量
|
|
|
-const (
|
|
|
- UnreadMultiplier = 10000.0 // 未读消息数乘数,用于小数部分存储
|
|
|
-)
|
|
|
-
|
|
|
-// calculateScore 计算有序集合的分数,同时存储时间戳和未读消息数
|
|
|
-// 整数部分:Unix时间戳,小数部分:未读消息数(除以乘数)
|
|
|
-func calculateScore(timestamp int64, unreadCount int) float64 {
|
|
|
- score := float64(timestamp) + (float64(unreadCount) / UnreadMultiplier)
|
|
|
- fmt.Printf("calculateScore: timestamp=%d, unreadCount=%d, score=%f\n", timestamp, unreadCount, score)
|
|
|
- return score
|
|
|
-}
|
|
|
-
|
|
|
-// parseScore 从分数中解析出时间戳和未读消息数
|
|
|
-func parseScore(score float64) (timestamp int64, unreadCount int) {
|
|
|
- timestamp = int64(score) // 整数部分是时间戳
|
|
|
- decimalPart := score - float64(timestamp)
|
|
|
- // 使用四舍五入来处理浮点数精度问题
|
|
|
- unreadCount = int(decimalPart*UnreadMultiplier + 0.5)
|
|
|
- fmt.Printf("parseScore: score=%f, timestamp=%d, decimalPart=%f, unreadCount=%d\n", score, timestamp, decimalPart, unreadCount)
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-func main() {
|
|
|
- fmt.Println("=== 分数计算测试 ===")
|
|
|
-
|
|
|
- // 测试用例
|
|
|
- timestamp := time.Now().Unix()
|
|
|
- fmt.Printf("原始时间戳: %d\n\n", timestamp)
|
|
|
-
|
|
|
- // 测试1: 未读消息数为0
|
|
|
- fmt.Println("测试1: 未读消息数为0")
|
|
|
- score1 := calculateScore(timestamp, 0)
|
|
|
- ts1, unread1 := parseScore(score1)
|
|
|
- fmt.Printf("结果: timestamp=%d, unreadCount=%d\n\n", ts1, unread1)
|
|
|
-
|
|
|
- // 测试2: 未读消息数为1
|
|
|
- fmt.Println("测试2: 未读消息数为1")
|
|
|
- score2 := calculateScore(timestamp, 1)
|
|
|
- ts2, unread2 := parseScore(score2)
|
|
|
- fmt.Printf("结果: timestamp=%d, unreadCount=%d\n\n", ts2, unread2)
|
|
|
-
|
|
|
- // 测试3: 未读消息数为5
|
|
|
- fmt.Println("测试3: 未读消息数为5")
|
|
|
- score3 := calculateScore(timestamp, 5)
|
|
|
- ts3, unread3 := parseScore(score3)
|
|
|
- fmt.Printf("结果: timestamp=%d, unreadCount=%d\n\n", ts3, unread3)
|
|
|
-
|
|
|
- // 测试4: 未读消息数为100
|
|
|
- fmt.Println("测试4: 未读消息数为100")
|
|
|
- score4 := calculateScore(timestamp, 100)
|
|
|
- ts4, unread4 := parseScore(score4)
|
|
|
- fmt.Printf("结果: timestamp=%d, unreadCount=%d\n\n", ts4, unread4)
|
|
|
-
|
|
|
- // 测试5: 验证小数部分的精度
|
|
|
- fmt.Println("测试5: 小数部分精度验证")
|
|
|
- testScore := 1703123456.123
|
|
|
- fmt.Printf("测试分数: %f\n", testScore)
|
|
|
- ts5, unread5 := parseScore(testScore)
|
|
|
- fmt.Printf("解析结果: timestamp=%d, unreadCount=%d\n\n", ts5, unread5)
|
|
|
-
|
|
|
- // 测试6: 字符串转换测试
|
|
|
- fmt.Println("测试6: 字符串转换测试")
|
|
|
- scoreStr := fmt.Sprintf("%.6f", score2)
|
|
|
- fmt.Printf("分数字符串: %s\n", scoreStr)
|
|
|
- if parsedScore, err := strconv.ParseFloat(scoreStr, 64); err == nil {
|
|
|
- ts6, unread6 := parseScore(parsedScore)
|
|
|
- fmt.Printf("字符串解析结果: timestamp=%d, unreadCount=%d\n", ts6, unread6)
|
|
|
- }
|
|
|
-}
|