util_test.go 12 KB

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