funcs_array_test.go 17 KB

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