util_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. package util
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "time"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestConvertParseLocalUnix(t *testing.T) {
  13. type args struct {
  14. unix int64
  15. }
  16. tests := []struct {
  17. Name string
  18. Args args
  19. }{
  20. {
  21. Name: "08:00:00",
  22. Args: args{
  23. unix: 1727049600,
  24. },
  25. },
  26. {
  27. Name: "09:00:00",
  28. Args: args{
  29. unix: 1727053200,
  30. },
  31. },
  32. }
  33. for _, tt := range tests {
  34. t.Run(tt.Name, func(t *testing.T) {
  35. got, err := ConvertParseLocalUnix(tt.Name)
  36. assert.Nil(t, err)
  37. assert.Equal(t, tt.Args.unix, got)
  38. })
  39. }
  40. }
  41. func TestGetLastDayOfMonth(t *testing.T) {
  42. tests := []struct {
  43. month string
  44. got string
  45. }{
  46. {
  47. month: "2022-02",
  48. got: "2022-02-28",
  49. },
  50. {
  51. month: "2023-02",
  52. got: "2023-02-28",
  53. },
  54. {
  55. month: "2024-02",
  56. got: "2024-02-29",
  57. },
  58. {
  59. month: "2024-10",
  60. got: "2024-10-31",
  61. }, {
  62. month: "2024-09",
  63. got: "2024-09-30",
  64. },
  65. }
  66. for _, tt := range tests {
  67. t.Run(tt.month, func(t *testing.T) {
  68. got, err := GetLastDayOfMonth(tt.month)
  69. assert.Nil(t, err)
  70. assert.Equal(t, tt.got, got)
  71. })
  72. }
  73. }
  74. func TestGetMonthsInRange(t *testing.T) {
  75. tests := []struct {
  76. startMonth string
  77. endMonth string
  78. got []string
  79. }{
  80. {
  81. startMonth: "2023-01",
  82. endMonth: "2023-03",
  83. got: []string{
  84. "2023-01",
  85. "2023-02",
  86. "2023-03",
  87. },
  88. },
  89. {
  90. startMonth: "2023-01",
  91. endMonth: "2023-12",
  92. got: []string{
  93. "2023-01",
  94. "2023-02",
  95. "2023-03",
  96. "2023-04",
  97. "2023-05",
  98. "2023-06",
  99. "2023-07",
  100. "2023-08",
  101. "2023-09",
  102. "2023-10",
  103. "2023-11",
  104. "2023-12",
  105. },
  106. },
  107. {
  108. startMonth: "2023-01",
  109. endMonth: "2023-01",
  110. got: []string{
  111. "2023-01",
  112. },
  113. },
  114. {
  115. startMonth: "2023-01",
  116. endMonth: "2023-01",
  117. got: []string{
  118. "2023-01",
  119. },
  120. },
  121. }
  122. for _, tt := range tests {
  123. t.Run(tt.startMonth, func(t *testing.T) {
  124. got, err := GetMonthsInRange(tt.startMonth, tt.endMonth)
  125. assert.Nil(t, err)
  126. assert.Equal(t, tt.got, got)
  127. })
  128. }
  129. }
  130. func TestRoundToTwoDecimals(t *testing.T) {
  131. tests := []struct {
  132. exp float64
  133. got float64
  134. }{
  135. {
  136. exp: 123.456,
  137. got: 123.46,
  138. },
  139. {
  140. exp: 123.455,
  141. got: 123.46,
  142. },
  143. {
  144. exp: 123.444,
  145. got: 123.44,
  146. },
  147. {
  148. exp: 123.4551,
  149. got: 123.46,
  150. },
  151. {
  152. exp: 123.4449,
  153. got: 123.44,
  154. },
  155. {
  156. exp: 0,
  157. got: 0,
  158. },
  159. {
  160. exp: 0.2,
  161. got: 0.2,
  162. },
  163. }
  164. for _, tt := range tests {
  165. t.Run("TestRoundToTwoDecimals", func(t *testing.T) {
  166. got := RoundToTwoDecimals(tt.exp)
  167. assert.Equal(t, tt.got, got)
  168. })
  169. }
  170. }
  171. func TestGet21DayPeriods(t *testing.T) {
  172. tests := []struct {
  173. startDay string
  174. endDay string
  175. got struct {
  176. DateRange [][]string
  177. Day int32
  178. MiddleDayString []string
  179. }
  180. }{
  181. {
  182. startDay: "2023-01-01",
  183. endDay: "2023-01-02",
  184. got: struct {
  185. DateRange [][]string
  186. Day int32
  187. MiddleDayString []string
  188. }{
  189. DateRange: [][]string{
  190. {"2022-12-13", "2023-01-02"},
  191. },
  192. Day: 11,
  193. MiddleDayString: []string{"2022-12-23"},
  194. },
  195. },
  196. {
  197. startDay: "2024-10-23",
  198. endDay: "2024-10-24",
  199. got: struct {
  200. DateRange [][]string
  201. Day int32
  202. MiddleDayString []string
  203. }{
  204. DateRange: [][]string{
  205. {"2024-10-04", "2024-10-24"},
  206. },
  207. Day: 11,
  208. MiddleDayString: []string{"2024-10-14"},
  209. },
  210. },
  211. {
  212. startDay: "2024-06-22",
  213. endDay: "2024-10-24",
  214. got: struct {
  215. DateRange [][]string
  216. Day int32
  217. MiddleDayString []string
  218. }{
  219. DateRange: [][]string{
  220. {"2024-06-21", "2024-07-11"},
  221. {"2024-07-12", "2024-08-01"},
  222. {"2024-08-02", "2024-08-22"},
  223. {"2024-08-23", "2024-09-12"},
  224. {"2024-09-13", "2024-10-03"},
  225. {"2024-10-04", "2024-10-24"},
  226. }, Day: 11,
  227. MiddleDayString: []string{"2024-07-01", "2024-07-22", "2024-08-12", "2024-09-02", "2024-09-23", "2024-10-14"},
  228. },
  229. },
  230. }
  231. for _, tt := range tests {
  232. t.Run(tt.startDay, func(t *testing.T) {
  233. got, err := Get21DayPeriods(tt.startDay, tt.endDay)
  234. assert.Nil(t, err)
  235. assert.Equal(t, tt.got.DateRange, got)
  236. for i, v := range tt.got.DateRange {
  237. middleDate, err := GetRangeDayMiddleDay(v, tt.got.Day)
  238. assert.Nil(t, err)
  239. assert.Equal(t, tt.got.MiddleDayString[i], middleDate)
  240. }
  241. })
  242. }
  243. }
  244. func TestGetRangeDayByDays(t *testing.T) {
  245. tests := []struct {
  246. startDay string
  247. endDay string
  248. days int32
  249. got [][]string
  250. }{
  251. {
  252. startDay: "2024-10-23",
  253. endDay: "2024-10-24",
  254. days: 5,
  255. got: [][]string{
  256. {"2024-10-23", "2024-10-24"},
  257. },
  258. },
  259. {
  260. startDay: "2024-10-23",
  261. endDay: "2024-10-24",
  262. days: 1,
  263. got: [][]string{
  264. {"2024-10-23", "2024-10-23"},
  265. {"2024-10-24", "2024-10-24"},
  266. },
  267. },
  268. {
  269. startDay: "2024-10-01",
  270. endDay: "2024-10-31",
  271. days: 7,
  272. got: [][]string{
  273. {"2024-10-01", "2024-10-07"},
  274. {"2024-10-08", "2024-10-14"},
  275. {"2024-10-15", "2024-10-21"},
  276. {"2024-10-22", "2024-10-28"},
  277. {"2024-10-29", "2024-10-31"},
  278. },
  279. },
  280. {
  281. startDay: "2024-10-01",
  282. endDay: "2024-10-31",
  283. days: 5,
  284. got: [][]string{
  285. {"2024-10-01", "2024-10-05"},
  286. {"2024-10-06", "2024-10-10"},
  287. {"2024-10-11", "2024-10-15"},
  288. {"2024-10-16", "2024-10-20"},
  289. {"2024-10-21", "2024-10-25"},
  290. {"2024-10-26", "2024-10-30"},
  291. {"2024-10-31", "2024-10-31"},
  292. },
  293. },
  294. }
  295. for _, tt := range tests {
  296. t.Run(tt.startDay, func(t *testing.T) {
  297. got, err := GetRangeDayByDays(tt.startDay, tt.endDay, tt.days)
  298. assert.Nil(t, err)
  299. assert.Equal(t, tt.got, got)
  300. })
  301. }
  302. }
  303. func TestConfidenceInterval2(t *testing.T) {
  304. tests := []struct {
  305. p float64
  306. total float64
  307. min float64
  308. max float64
  309. }{
  310. {
  311. p: 0.38,
  312. total: 114,
  313. min: 29,
  314. max: 47,
  315. },
  316. {
  317. p: 0.49,
  318. total: 142,
  319. min: 40,
  320. max: 58,
  321. },
  322. {
  323. p: 0.41,
  324. total: 125,
  325. min: 32,
  326. max: 50,
  327. },
  328. {
  329. p: 0,
  330. total: 0,
  331. min: 0,
  332. max: 0,
  333. },
  334. }
  335. for _, tt := range tests {
  336. min, max := ConfidenceInterval2(tt.p, tt.total)
  337. assert.Equal(t, tt.min, min)
  338. assert.Equal(t, tt.max, max)
  339. }
  340. }
  341. func TestRemoveDuplicates(t *testing.T) {
  342. tests := []struct {
  343. actual []string
  344. got []string
  345. }{
  346. {
  347. actual: []string{
  348. "1", "1",
  349. },
  350. got: []string{
  351. "1",
  352. },
  353. },
  354. {
  355. actual: []string{
  356. "1", "1", "2",
  357. },
  358. got: []string{
  359. "1", "2",
  360. },
  361. },
  362. {
  363. actual: []string{
  364. "1",
  365. },
  366. got: []string{
  367. "1",
  368. },
  369. },
  370. {
  371. actual: []string{},
  372. got: []string{},
  373. },
  374. }
  375. for _, tt := range tests {
  376. got := RemoveDuplicates(tt.actual)
  377. assert.Equal(t, tt.got, got)
  378. }
  379. }
  380. func TestDaysBetween(t *testing.T) {
  381. tests := []struct {
  382. actual []int64
  383. got int64
  384. }{
  385. {
  386. actual: []int64{
  387. 1730736000, // 2024-11-05 00:00:00
  388. 1730772000, // 2024-11-05 10:00:00
  389. },
  390. got: 0,
  391. },
  392. {
  393. actual: []int64{
  394. 1730772000, // 2024-11-05 10:00:00
  395. 1730908800, // 2024-11-07 00:00:00
  396. },
  397. got: 2,
  398. },
  399. {
  400. actual: []int64{
  401. 1730908800, // 2024-11-07 00:00:00
  402. 1730772000, // 2024-11-05 10:00:00
  403. },
  404. got: -2,
  405. },
  406. }
  407. for _, v := range tests {
  408. got := DaysBetween(v.actual[0], v.actual[1])
  409. assert.Equal(t, got, v.got)
  410. }
  411. }
  412. func TestGetNeckRingActiveTimer(t *testing.T) {
  413. nowTime := time.Now().Format(Layout)
  414. tests := struct {
  415. frameId []int32
  416. dateTime []string
  417. hours []int32
  418. }{
  419. frameId: []int32{
  420. 1, 2, 3, 4, 5, 6,
  421. 11, 12, 13, 14, 15, 16,
  422. 21, 22, 23, 24, 25, 26,
  423. 31, 32, 33, 34, 35, 36,
  424. 41, 42, 43, 44, 45, 46,
  425. 51, 52, 53, 54, 55, 56,
  426. 61, 62, 63, 64, 65, 66,
  427. 71, 72, 73, 74, 75, 76,
  428. 81, 82, 83, 84, 85, 86,
  429. 91, 92, 93, 94, 95, 96,
  430. 101, 102, 103, 104, 105, 106,
  431. 111, 112, 113, 114, 115, 116,
  432. 8, 18, 28, 38, 48, 58, 68, 78, 88, 98, 108, 118,
  433. },
  434. hours: []int32{
  435. 0, 0, 1, 1, 1, 2,
  436. 2, 2, 3, 3, 3, 4,
  437. 4, 4, 5, 5, 5, 6,
  438. 6, 6, 7, 7, 7, 8,
  439. 8, 8, 9, 9, 9, 10,
  440. 10, 10, 11, 11, 11, 12,
  441. 12, 12, 13, 13, 13, 14,
  442. 14, 14, 15, 15, 15, 16,
  443. 16, 16, 17, 17, 17, 18,
  444. 18, 18, 19, 19, 19, 20,
  445. 20, 20, 21, 21, 21, 22,
  446. 22, 22, 23, 23, 23, 24,
  447. 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 0,
  448. },
  449. dateTime: []string{
  450. fmt.Sprintf("%s 00:20:00", nowTime), fmt.Sprintf("%s 00:40:00", nowTime), fmt.Sprintf("%s 01:00:00", nowTime),
  451. fmt.Sprintf("%s 01:20:00", nowTime), fmt.Sprintf("%s 01:40:00", nowTime), fmt.Sprintf("%s 02:00:00", nowTime),
  452. fmt.Sprintf("%s 02:20:00", nowTime), fmt.Sprintf("%s 02:40:00", nowTime), fmt.Sprintf("%s 03:00:00", nowTime),
  453. fmt.Sprintf("%s 03:20:00", nowTime), fmt.Sprintf("%s 03:40:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime),
  454. fmt.Sprintf("%s 04:20:00", nowTime), fmt.Sprintf("%s 04:40:00", nowTime), fmt.Sprintf("%s 05:00:00", nowTime),
  455. fmt.Sprintf("%s 05:20:00", nowTime), fmt.Sprintf("%s 05:40:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime),
  456. fmt.Sprintf("%s 06:20:00", nowTime), fmt.Sprintf("%s 06:40:00", nowTime), fmt.Sprintf("%s 07:00:00", nowTime),
  457. fmt.Sprintf("%s 07:20:00", nowTime), fmt.Sprintf("%s 07:40:00", nowTime), fmt.Sprintf("%s 08:00:00", nowTime),
  458. fmt.Sprintf("%s 08:20:00", nowTime), fmt.Sprintf("%s 08:40:00", nowTime), fmt.Sprintf("%s 09:00:00", nowTime),
  459. fmt.Sprintf("%s 09:20:00", nowTime), fmt.Sprintf("%s 09:40:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime),
  460. fmt.Sprintf("%s 10:20:00", nowTime), fmt.Sprintf("%s 10:40:00", nowTime), fmt.Sprintf("%s 11:00:00", nowTime),
  461. fmt.Sprintf("%s 11:20:00", nowTime), fmt.Sprintf("%s 11:40:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime),
  462. fmt.Sprintf("%s 12:20:00", nowTime), fmt.Sprintf("%s 12:40:00", nowTime), fmt.Sprintf("%s 13:00:00", nowTime),
  463. fmt.Sprintf("%s 13:20:00", nowTime), fmt.Sprintf("%s 13:40:00", nowTime), fmt.Sprintf("%s 14:00:00", nowTime),
  464. fmt.Sprintf("%s 14:20:00", nowTime), fmt.Sprintf("%s 14:40:00", nowTime), fmt.Sprintf("%s 15:00:00", nowTime),
  465. fmt.Sprintf("%s 15:20:00", nowTime), fmt.Sprintf("%s 15:40:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime),
  466. fmt.Sprintf("%s 16:20:00", nowTime), fmt.Sprintf("%s 16:40:00", nowTime), fmt.Sprintf("%s 17:00:00", nowTime),
  467. fmt.Sprintf("%s 17:20:00", nowTime), fmt.Sprintf("%s 17:40:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime),
  468. fmt.Sprintf("%s 18:20:00", nowTime), fmt.Sprintf("%s 18:40:00", nowTime), fmt.Sprintf("%s 19:00:00", nowTime),
  469. fmt.Sprintf("%s 19:20:00", nowTime), fmt.Sprintf("%s 19:40:00", nowTime), fmt.Sprintf("%s 20:00:00", nowTime),
  470. fmt.Sprintf("%s 20:20:00", nowTime), fmt.Sprintf("%s 20:40:00", nowTime), fmt.Sprintf("%s 21:00:00", nowTime),
  471. fmt.Sprintf("%s 21:20:00", nowTime), fmt.Sprintf("%s 21:40:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime),
  472. fmt.Sprintf("%s 22:20:00", nowTime), fmt.Sprintf("%s 22:40:00", nowTime), fmt.Sprintf("%s 23:00:00", nowTime),
  473. fmt.Sprintf("%s 23:20:00", nowTime), fmt.Sprintf("%s 23:40:00", nowTime), fmt.Sprintf("%s 24:00:00", nowTime),
  474. fmt.Sprintf("%s 02:00:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime),
  475. fmt.Sprintf("%s 08:00:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime),
  476. fmt.Sprintf("%s 14:00:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime),
  477. fmt.Sprintf("%s 20:00:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime), fmt.Sprintf("%s 00:00:00", nowTime),
  478. },
  479. }
  480. for i, frameId := range tests.frameId {
  481. got, hours := GetNeckRingActiveTimer(frameId)
  482. t.Logf("frameId: %d, test-hours: %d,hours:%d got :%s", frameId, tests.hours[i], hours, got)
  483. //assert.Equal(t, got, tests.dateTime[i])
  484. assert.Equal(t, int32(hours), tests.hours[i])
  485. }
  486. }
  487. func TestSubstr(t *testing.T) {
  488. nowTime := time.Now().Format(Layout)
  489. text := `102053 0 0 53 12.2 4.7 101415 101538 0.4 0 0 0 ???????? 000000 0.0 0.0 0.0 0.0 0`
  490. fmt.Println()
  491. detachTime := fmt.Sprintf("%s %s:%s:%s", nowTime, Substr(text, 0, 2), Substr(text, 2, 2), Substr(text, 4, 2))
  492. varCowCode := fmt.Sprintf("%s", Substr(text, 7, 6))
  493. detacherAddress := fmt.Sprintf("%s", Substr(text, 20, 2))
  494. milkWeight := fmt.Sprintf("%s", strings.TrimSpace(Substr(text, 25, 4)))
  495. s1 := strings.TrimSpace(Substr(text, 0, 6))
  496. s2 := strings.TrimSpace(Substr(text, 25, 4))
  497. fmt.Println("detachTime", detachTime, varCowCode, detacherAddress, milkWeight, s1, s2)
  498. fmt.Println("milkWeight:", milkWeight, strings.TrimSpace(Substr(text, 25, 4)))
  499. milkWeight1, err := strconv.ParseInt(strings.TrimSpace(Substr(text, 25, 4)), 10, 64)
  500. fmt.Println("milkWeight1:", milkWeight1, err)
  501. }
  502. func Test_demo(t *testing.T) {
  503. initialTime, err := TimeParseLocal(LayoutTime, "")
  504. fmt.Println(initialTime, err)
  505. type MyInt pasturePb.Week_Kind
  506. nums := []MyInt{3, 1, 4, 5, 2, 6}
  507. // 使用sort.Slice排序
  508. sort.Slice(nums, func(i, j int) bool {
  509. return nums[i] < nums[j]
  510. })
  511. fmt.Println(nums) // 输出: [1 1 2 3 4 5 6 9]
  512. }