funcs_array_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  24. "github.com/lf-edge/ekuiper/internal/topo/state"
  25. "github.com/lf-edge/ekuiper/pkg/api"
  26. "github.com/lf-edge/ekuiper/pkg/ast"
  27. )
  28. func TestArrayCommonFunctions(t *testing.T) {
  29. contextLogger := conf.Log.WithField("rule", "testExec")
  30. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  31. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  32. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  33. tests := []struct {
  34. name string
  35. args []interface{}
  36. result interface{}
  37. }{
  38. {
  39. name: "array_create",
  40. args: []interface{}{
  41. 1, "2", 3,
  42. },
  43. result: []interface{}{
  44. 1, "2", 3,
  45. },
  46. },
  47. {
  48. name: "array_position",
  49. args: []interface{}{
  50. 1, 2,
  51. },
  52. result: errorArrayFirstArgumentNotArrayError,
  53. },
  54. {
  55. name: "array_position",
  56. args: []interface{}{
  57. []interface{}{3, 2, 1},
  58. 1,
  59. },
  60. result: 2,
  61. },
  62. {
  63. name: "array_position",
  64. args: []interface{}{
  65. []interface{}{3, 2, 1},
  66. 4,
  67. },
  68. result: -1,
  69. },
  70. {
  71. name: "length",
  72. args: []interface{}{
  73. []interface{}{1, 2, 3},
  74. },
  75. result: 3,
  76. },
  77. {
  78. name: "element_at",
  79. args: []interface{}{
  80. 1, 2,
  81. },
  82. result: fmt.Errorf("first argument should be []interface{} or map[string]interface{}"),
  83. },
  84. {
  85. name: "element_at",
  86. args: []interface{}{
  87. []interface{}{1, 2, 3}, 4,
  88. },
  89. result: errorArrayIndex,
  90. },
  91. {
  92. name: "element_at",
  93. args: []interface{}{
  94. []interface{}{1, 2, 3}, -4,
  95. },
  96. result: errorArrayIndex,
  97. },
  98. {
  99. name: "element_at",
  100. args: []interface{}{
  101. []interface{}{1, 2, 3}, 1,
  102. },
  103. result: 2,
  104. },
  105. {
  106. name: "element_at",
  107. args: []interface{}{
  108. []interface{}{1, 2, 3}, -1,
  109. },
  110. result: 3,
  111. },
  112. {
  113. name: "array_contains",
  114. args: []interface{}{
  115. 1, 2,
  116. },
  117. result: errorArrayFirstArgumentNotArrayError,
  118. },
  119. {
  120. name: "array_contains",
  121. args: []interface{}{
  122. []interface{}{1, 2}, 2,
  123. },
  124. result: true,
  125. },
  126. {
  127. name: "array_contains",
  128. args: []interface{}{
  129. []interface{}{1, 2}, 3,
  130. },
  131. result: false,
  132. },
  133. {
  134. name: "array_remove",
  135. args: []interface{}{
  136. []interface{}{3, 1, 2}, 1,
  137. },
  138. result: []interface{}{3, 2},
  139. },
  140. {
  141. name: "array_remove",
  142. args: []interface{}{
  143. []interface{}{'a', 'b', 'c'}, 'c',
  144. },
  145. result: []interface{}{'a', 'b'},
  146. },
  147. {
  148. name: "array_remove",
  149. args: []interface{}{
  150. []interface{}{1, 2, 3, 4, 3}, 3,
  151. },
  152. result: []interface{}{1, 2, 4},
  153. },
  154. {
  155. name: "array_remove",
  156. args: []interface{}{
  157. []interface{}{3, 3, 3}, 3,
  158. },
  159. result: []interface{}{},
  160. },
  161. {
  162. name: "array_last_position",
  163. args: []interface{}{
  164. []interface{}{5, nil, 5}, 5,
  165. },
  166. result: 2,
  167. },
  168. {
  169. name: "array_last_position",
  170. args: []interface{}{
  171. []interface{}{5, nil, 5}, "hello",
  172. },
  173. result: -1,
  174. },
  175. {
  176. name: "array_last_position",
  177. args: []interface{}{
  178. []interface{}{5, nil, 7}, 5,
  179. },
  180. result: 0,
  181. },
  182. {
  183. name: "array_last_position",
  184. args: []interface{}{
  185. 1, 2,
  186. },
  187. result: errorArrayFirstArgumentNotArrayError,
  188. },
  189. {
  190. name: "array_last_position",
  191. args: []interface{}{
  192. []interface{}{5, "hello", nil}, nil,
  193. },
  194. result: 2,
  195. },
  196. {
  197. name: "array_contains_any",
  198. args: []interface{}{
  199. []interface{}{1, 2, 3}, []interface{}{0, 2, 4},
  200. },
  201. result: true,
  202. },
  203. {
  204. name: "array_contains_any",
  205. args: []interface{}{
  206. []interface{}{1, 2, 3}, []interface{}{4, "hello", 6},
  207. },
  208. result: false,
  209. },
  210. {
  211. name: "array_contains_any",
  212. args: []interface{}{
  213. []interface{}{1, 2, 3}, []interface{}{},
  214. },
  215. result: false,
  216. },
  217. {
  218. name: "array_contains_any",
  219. args: []interface{}{
  220. []interface{}{1, 2, 3, 4}, 1,
  221. },
  222. result: errorArraySecondArgumentNotArrayError,
  223. },
  224. {
  225. name: "array_intersect",
  226. args: []interface{}{
  227. []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
  228. },
  229. result: []interface{}{1, 3},
  230. },
  231. {
  232. name: "array_intersect",
  233. args: []interface{}{
  234. []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
  235. },
  236. result: []interface{}{"ekuiper", 3},
  237. },
  238. {
  239. name: "array_intersect",
  240. args: []interface{}{
  241. []interface{}{"hello", "ekuiper", 2, 3}, "ekuiper",
  242. },
  243. result: errorArraySecondArgumentNotArrayError,
  244. },
  245. {
  246. name: "array_intersect",
  247. args: []interface{}{
  248. "1", []interface{}{1, 2, 3},
  249. },
  250. result: errorArrayFirstArgumentNotArrayError,
  251. },
  252. {
  253. name: "array_union",
  254. args: []interface{}{
  255. []interface{}{1, 1, 2, 3}, []interface{}{1, 3, 4},
  256. },
  257. result: []interface{}{1, 2, 3, 4},
  258. },
  259. {
  260. name: "array_union",
  261. args: []interface{}{
  262. []interface{}{"hello", "ekuiper", 2, 3}, []interface{}{"ekuiper", 3, 4},
  263. },
  264. result: []interface{}{"hello", "ekuiper", 2, 3, 4},
  265. },
  266. {
  267. name: "array_union",
  268. args: []interface{}{
  269. []interface{}{1, 1, 2, 3}, "ekuiper",
  270. },
  271. result: errorArraySecondArgumentNotArrayError,
  272. },
  273. {
  274. name: "array_union",
  275. args: []interface{}{
  276. "1", []interface{}{1, 2, 3},
  277. },
  278. result: errorArrayFirstArgumentNotArrayError,
  279. },
  280. {
  281. name: "array_max",
  282. args: []interface{}{
  283. []interface{}{1},
  284. },
  285. result: int64(1),
  286. },
  287. {
  288. name: "array_max",
  289. args: []interface{}{
  290. []interface{}{1, nil, 3},
  291. },
  292. result: int64(3),
  293. },
  294. {
  295. name: "array_max",
  296. args: []interface{}{
  297. []interface{}{1, "4", 3},
  298. },
  299. result: errors.New("requires int64 but found string(4)"),
  300. },
  301. {
  302. name: "array_max",
  303. args: []interface{}{
  304. []interface{}{1, "a4a", 3},
  305. },
  306. result: errors.New("requires int64 but found string(a4a)"),
  307. },
  308. {
  309. name: "array_max",
  310. args: []interface{}{
  311. []interface{}{1.2, 4.2, 3.0},
  312. },
  313. result: 4.2,
  314. },
  315. {
  316. name: "array_max",
  317. args: []interface{}{
  318. []interface{}{1, 3.2, 4.1, 2},
  319. },
  320. result: int64(4),
  321. },
  322. {
  323. name: "array_min",
  324. args: []interface{}{
  325. []interface{}{1, nil, 3},
  326. },
  327. result: int64(1),
  328. },
  329. {
  330. name: "array_min",
  331. args: []interface{}{
  332. []interface{}{1, "0", 3},
  333. },
  334. result: errors.New("requires int64 but found string(0)"),
  335. },
  336. {
  337. name: "array_min",
  338. args: []interface{}{
  339. []interface{}{1.2, 4.2, 3.0},
  340. },
  341. result: 1.2,
  342. },
  343. {
  344. name: "array_min",
  345. args: []interface{}{
  346. []interface{}{1, "a4a", 3},
  347. },
  348. result: errors.New("requires int64 but found string(a4a)"),
  349. },
  350. {
  351. name: "array_min",
  352. args: []interface{}{
  353. []interface{}{1, 3.2, 4.1, 2},
  354. },
  355. result: int64(1),
  356. },
  357. {
  358. name: "array_except",
  359. args: []interface{}{
  360. []interface{}{1, 2, 3}, []interface{}{1, 3, 4},
  361. },
  362. result: []interface{}{2},
  363. },
  364. {
  365. name: "array_except",
  366. args: []interface{}{
  367. []interface{}{1, 2, 3}, []interface{}{4, 5, 6},
  368. },
  369. result: []interface{}{1, 2, 3},
  370. },
  371. {
  372. name: "array_except",
  373. args: []interface{}{
  374. []interface{}{1, 2, 1, 2, 3}, []interface{}{1, 2, 1, 1, 2, 2, 4},
  375. },
  376. result: []interface{}{3},
  377. },
  378. {
  379. name: "array_except",
  380. args: []interface{}{
  381. []interface{}{1, 1, 1, 1, 3}, []interface{}{4},
  382. },
  383. result: []interface{}{1, 3},
  384. },
  385. {
  386. name: "repeat",
  387. args: []interface{}{
  388. 1, 5,
  389. },
  390. result: []interface{}{1, 1, 1, 1, 1},
  391. },
  392. {
  393. name: "repeat",
  394. args: []interface{}{
  395. 1, "hellow",
  396. },
  397. result: errorArraySecondArgumentNotIntError,
  398. },
  399. {
  400. name: "repeat",
  401. args: []interface{}{
  402. "hello", 3,
  403. },
  404. result: []interface{}{"hello", "hello", "hello"},
  405. },
  406. {
  407. name: "repeat",
  408. args: []interface{}{
  409. "rockset", 0,
  410. },
  411. result: []interface{}{},
  412. },
  413. {
  414. name: "sequence",
  415. args: []interface{}{
  416. 1, 5,
  417. },
  418. result: []interface{}{1, 2, 3, 4, 5},
  419. },
  420. {
  421. name: "sequence",
  422. args: []interface{}{
  423. "1", 10, 2,
  424. },
  425. result: errorArrayFirstArgumentNotIntError,
  426. },
  427. {
  428. name: "sequence",
  429. args: []interface{}{
  430. 1, "6", 2,
  431. },
  432. result: errorArraySecondArgumentNotIntError,
  433. },
  434. {
  435. name: "sequence",
  436. args: []interface{}{
  437. 1, 7, "1",
  438. },
  439. result: errorArrayThirdArgumentNotIntError,
  440. },
  441. {
  442. name: "sequence",
  443. args: []interface{}{
  444. 1, 10, 2,
  445. },
  446. result: []interface{}{1, 3, 5, 7, 9},
  447. },
  448. {
  449. name: "sequence",
  450. args: []interface{}{
  451. 10, 1, -3,
  452. },
  453. result: []interface{}{10, 7, 4, 1},
  454. },
  455. {
  456. name: "array_cardinality",
  457. args: []interface{}{
  458. []interface{}{1, 2, 3},
  459. },
  460. result: 3,
  461. },
  462. {
  463. name: "array_cardinality",
  464. args: []interface{}{
  465. 1, 2, 3,
  466. },
  467. result: errorArrayFirstArgumentNotArrayError,
  468. },
  469. {
  470. name: "array_flatten",
  471. args: []interface{}{
  472. []interface{}{
  473. []interface{}{1, 2, 3},
  474. },
  475. },
  476. result: []interface{}{1, 2, 3},
  477. },
  478. {
  479. name: "array_flatten",
  480. args: []interface{}{
  481. 1, 2,
  482. },
  483. result: errorArrayFirstArgumentNotArrayError,
  484. },
  485. {
  486. name: "array_flatten",
  487. args: []interface{}{
  488. []interface{}{1, 2, 3}, 4,
  489. },
  490. result: errorArrayNotArrayElementError,
  491. },
  492. {
  493. name: "array_flatten",
  494. args: []interface{}{
  495. []interface{}{
  496. []interface{}{1, 2, 3},
  497. []interface{}{4, 5, 6},
  498. },
  499. },
  500. result: []interface{}{1, 2, 3, 4, 5, 6},
  501. },
  502. {
  503. name: "array_distinct",
  504. args: []interface{}{
  505. []interface{}{1, 2, 3},
  506. },
  507. result: []interface{}{1, 2, 3},
  508. },
  509. {
  510. name: "array_distinct",
  511. args: []interface{}{
  512. 1, 1,
  513. },
  514. result: errorArrayFirstArgumentNotArrayError,
  515. },
  516. {
  517. name: "array_distinct",
  518. args: []interface{}{
  519. []interface{}{1, 1, 1},
  520. },
  521. result: []interface{}{1},
  522. },
  523. {
  524. name: "array_distinct",
  525. args: []interface{}{
  526. []interface{}{1, 2, 2, 1},
  527. },
  528. result: []interface{}{1, 2},
  529. },
  530. {
  531. name: "array_map",
  532. args: []interface{}{
  533. "round", []interface{}{0, 0.4, 1.2},
  534. },
  535. result: []interface{}{0.0, 0.0, 1.0},
  536. },
  537. {
  538. name: "array_map",
  539. args: []interface{}{
  540. 123, []interface{}{1, 2, 3},
  541. },
  542. result: errorArrayFirstArgumentNotStringError,
  543. },
  544. {
  545. name: "array_map",
  546. args: []interface{}{
  547. "round", 1,
  548. },
  549. result: errorArraySecondArgumentNotArrayError,
  550. },
  551. {
  552. name: "array_map",
  553. args: []interface{}{
  554. "abs", []interface{}{0, -0.4, 1.2},
  555. },
  556. result: []interface{}{0, 0.4, 1.2},
  557. },
  558. {
  559. name: "array_map",
  560. args: []interface{}{
  561. "pow", []interface{}{0, -0.4, 1.2},
  562. },
  563. result: fmt.Errorf("validate function arguments failed."),
  564. },
  565. {
  566. name: "array_map",
  567. args: []interface{}{
  568. "avg", []interface{}{0, -0.4, 1.2},
  569. },
  570. result: fmt.Errorf("first argument should be a scalar function."),
  571. },
  572. {
  573. name: "array_map",
  574. args: []interface{}{
  575. "ceil", []interface{}{0, -1, 1.2},
  576. },
  577. result: []interface{}{0.0, -1.0, 2.0},
  578. },
  579. {
  580. name: "array_map",
  581. args: []interface{}{
  582. "power", []interface{}{1, 2, 3},
  583. },
  584. result: fmt.Errorf("validate function arguments failed."),
  585. },
  586. {
  587. name: "array_join",
  588. args: []interface{}{
  589. "a", "",
  590. },
  591. result: errorArrayFirstArgumentNotArrayError,
  592. },
  593. {
  594. name: "array_join",
  595. args: []interface{}{
  596. []interface{}{"a", "b", "c"}, 123, "a",
  597. },
  598. result: errorArraySecondArgumentNotStringError,
  599. },
  600. {
  601. name: "array_join",
  602. args: []interface{}{
  603. []interface{}{"a", "b", "c"}, ":", 123,
  604. },
  605. result: errorArrayThirdArgumentNotStringError,
  606. },
  607. {
  608. name: "array_join",
  609. args: []interface{}{
  610. []interface{}{123, "b", "c"}, ":", "a",
  611. },
  612. result: errorArrayNotStringElementError,
  613. },
  614. {
  615. name: "array_join",
  616. args: []interface{}{
  617. []interface{}{"a", "b", "c"}, "",
  618. },
  619. result: "abc",
  620. },
  621. {
  622. name: "array_join",
  623. args: []interface{}{
  624. []interface{}{"a", nil, "b"}, ":",
  625. },
  626. result: "a:b",
  627. },
  628. {
  629. name: "array_join",
  630. args: []interface{}{
  631. []interface{}{"a", "b", "c"}, ":",
  632. },
  633. result: "a:b:c",
  634. },
  635. {
  636. name: "array_join",
  637. args: []interface{}{
  638. []interface{}{"a", "b", "c"}, ":,%",
  639. },
  640. result: "a:,%b:,%c",
  641. },
  642. {
  643. name: "array_join",
  644. args: []interface{}{
  645. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  646. },
  647. result: "a:nullReplacementStr:c",
  648. },
  649. {
  650. name: "array_join",
  651. args: []interface{}{
  652. []interface{}{"a", nil, "c"}, ":", "nullReplacementStr",
  653. },
  654. result: "a:nullReplacementStr:c",
  655. },
  656. {
  657. name: "array_join",
  658. args: []interface{}{
  659. []interface{}{"a", "b", "c"}, ":", "a",
  660. },
  661. result: "a:b:c",
  662. },
  663. {
  664. name: "array_join",
  665. args: []interface{}{
  666. []interface{}{"a", "b", "c"}, ":",
  667. },
  668. result: "a:b:c",
  669. },
  670. {
  671. name: "array_join",
  672. args: []interface{}{
  673. []interface{}{nil, nil, nil}, ",", "nullReplacementStr",
  674. },
  675. result: "nullReplacementStr,nullReplacementStr,nullReplacementStr",
  676. },
  677. {
  678. name: "array_join",
  679. args: []interface{}{
  680. []interface{}{nil, nil, nil}, ",",
  681. },
  682. result: "",
  683. },
  684. {
  685. name: "array_join",
  686. args: []interface{}{
  687. []interface{}{"a", "b", nil}, ",",
  688. },
  689. result: "a,b",
  690. },
  691. {
  692. name: "array_concat",
  693. args: []interface{}{
  694. []interface{}{1},
  695. []interface{}{2},
  696. []interface{}{"3"},
  697. []interface{}{nil},
  698. },
  699. result: []interface{}{
  700. 1, 2, "3", nil,
  701. },
  702. },
  703. {
  704. name: "array_concat",
  705. args: []interface{}{
  706. []interface{}{1},
  707. nil,
  708. },
  709. result: nil,
  710. },
  711. }
  712. fe := funcExecutor{}
  713. for _, tt := range tests {
  714. t.Run(tt.name, func(t *testing.T) {
  715. result, _ := fe.ExecWithName(tt.args, fctx, tt.name)
  716. assert.Equal(t, tt.result, result)
  717. })
  718. }
  719. }
  720. func TestArrayShuffle(t *testing.T) {
  721. contextLogger := conf.Log.WithField("rule", "testExec")
  722. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  723. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  724. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  725. tests := []struct {
  726. name string
  727. args []interface{}
  728. result []interface{}
  729. }{
  730. {
  731. name: "array_shuffle",
  732. args: []interface{}{
  733. []interface{}{1, 2, 3},
  734. },
  735. result: []interface{}{
  736. []interface{}{1, 2, 3}, []interface{}{1, 3, 2}, []interface{}{2, 1, 3}, []interface{}{2, 3, 1}, []interface{}{3, 1, 2}, []interface{}{3, 2, 1},
  737. },
  738. },
  739. {
  740. name: "array_shuffle",
  741. args: []interface{}{
  742. 1,
  743. },
  744. result: []interface{}{
  745. errorArrayFirstArgumentNotArrayError,
  746. },
  747. },
  748. }
  749. for i, tt := range tests {
  750. f, ok := builtins[tt.name]
  751. if !ok {
  752. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  753. }
  754. result, _ := f.exec(fctx, tt.args)
  755. flag := false
  756. for _, actual := range tt.result {
  757. if reflect.DeepEqual(result, actual) {
  758. flag = true
  759. break
  760. }
  761. }
  762. if !flag {
  763. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  764. }
  765. }
  766. }
  767. func TestArraySort(t *testing.T) {
  768. contextLogger := conf.Log.WithField("rule", "testExec")
  769. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  770. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  771. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  772. tests := []struct {
  773. name string
  774. args []interface{}
  775. result []interface{}
  776. }{
  777. {
  778. name: "array_sort",
  779. args: []any{
  780. []any{3, 2, 1},
  781. },
  782. result: []interface{}{1, 2, 3},
  783. },
  784. {
  785. name: "array_sort",
  786. args: []any{
  787. []any{3, 1.6, -0.83},
  788. },
  789. result: []interface{}{-0.83, 1.6, 3},
  790. },
  791. {
  792. name: "array_sort",
  793. args: []any{
  794. []any{"abc", 3, "def", 1.6, -0.83},
  795. },
  796. result: []interface{}{-0.83, 1.6, 3, "abc", "def"},
  797. },
  798. }
  799. for i, tt := range tests {
  800. f, ok := builtins[tt.name]
  801. if !ok {
  802. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  803. }
  804. result, _ := f.exec(fctx, tt.args)
  805. flag := false
  806. if reflect.DeepEqual(result, tt.result) {
  807. flag = true
  808. }
  809. if !flag {
  810. t.Errorf("%d result mismatch,\ngot:\t%v \nwant in:\t%v", i, result, tt.result)
  811. }
  812. }
  813. }
  814. func TestArrayFuncNil(t *testing.T) {
  815. contextLogger := conf.Log.WithField("rule", "testExec")
  816. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  817. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  818. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  819. oldBuiltins := builtins
  820. defer func() {
  821. builtins = oldBuiltins
  822. }()
  823. builtins = map[string]builtinFunc{}
  824. registerArrayFunc()
  825. for mathFuncName, mathFunc := range builtins {
  826. switch mathFuncName {
  827. case "array_create":
  828. r, b := mathFunc.exec(fctx, []interface{}{nil})
  829. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  830. require.Equal(t, r, nil, fmt.Sprintf("%v failed", mathFuncName))
  831. r, b = mathFunc.exec(fctx, []interface{}{nil, 1})
  832. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  833. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  834. case "array_position", "array_last_position":
  835. r, b := mathFunc.exec(fctx, []interface{}{nil})
  836. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  837. require.Equal(t, r, -1, fmt.Sprintf("%v failed", mathFuncName))
  838. case "array_contains", "array_contains_any":
  839. r, b := mathFunc.check([]interface{}{nil})
  840. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  841. require.False(t, r.(bool), fmt.Sprintf("%v failed", mathFuncName))
  842. case "array_union":
  843. r, b := mathFunc.exec(fctx, []interface{}{[]interface{}{1}, nil})
  844. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  845. require.Equal(t, r, []interface{}{1}, fmt.Sprintf("%v failed", mathFuncName))
  846. case "array_cardinality":
  847. r, b := mathFunc.check([]interface{}{nil})
  848. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  849. require.Equal(t, r, 0, fmt.Sprintf("%v failed", mathFuncName))
  850. default:
  851. r, b := mathFunc.check([]interface{}{nil})
  852. require.True(t, b, fmt.Sprintf("%v failed", mathFuncName))
  853. require.Nil(t, r, fmt.Sprintf("%v failed", mathFuncName))
  854. }
  855. }
  856. }
  857. func TestArrayFuncVal(t *testing.T) {
  858. tests := []struct {
  859. name string
  860. funcName string
  861. args []ast.Expr
  862. err error
  863. }{
  864. {
  865. name: "array sort failure",
  866. funcName: "array_sort",
  867. args: []ast.Expr{
  868. &ast.BooleanLiteral{Val: true},
  869. &ast.BooleanLiteral{Val: true},
  870. },
  871. err: fmt.Errorf("Expect 1 arguments but found 2."),
  872. },
  873. }
  874. for _, tt := range tests {
  875. t.Run(tt.name, func(t *testing.T) {
  876. f, ok := builtins[tt.funcName]
  877. assert.True(t, ok)
  878. err := f.val(nil, tt.args)
  879. assert.Equal(t, tt.err, err)
  880. })
  881. }
  882. }