How to use Check method of lib Package

Best K6 code snippet using lib.Check

cc_test.go

Source:cc_test.go Github

copy

Full Screen

...218 mod := ctx.ModuleForTests(name, vendorVariant).Module().(*Module)219 if !mod.hasVendorVariant() {220 t.Errorf("%q must have vendor variant", name)221 }222 // Check library properties.223 lib, ok := mod.compiler.(*libraryDecorator)224 if !ok {225 t.Errorf("%q must have libraryDecorator", name)226 } else if lib.baseInstaller.subDir != subDir {227 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,228 lib.baseInstaller.subDir)229 }230 // Check VNDK properties.231 if mod.vndkdep == nil {232 t.Fatalf("%q must have `vndkdep`", name)233 }234 if !mod.isVndk() {235 t.Errorf("%q isVndk() must equal to true", name)236 }237 if mod.isVndkSp() != isVndkSp {238 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)239 }240 // Check VNDK extension properties.241 isVndkExt := extends != ""242 if mod.isVndkExt() != isVndkExt {243 t.Errorf("%q isVndkExt() must equal to %t", name, isVndkExt)244 }245 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {246 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)247 }248}249func TestVndk(t *testing.T) {250 ctx := testCc(t, `251 cc_library {252 name: "libvndk",253 vendor_available: true,254 vndk: {255 enabled: true,256 },257 nocrt: true,258 }259 cc_library {260 name: "libvndk_private",261 vendor_available: false,262 vndk: {263 enabled: true,264 },265 nocrt: true,266 }267 cc_library {268 name: "libvndk_sp",269 vendor_available: true,270 vndk: {271 enabled: true,272 support_system_process: true,273 },274 nocrt: true,275 }276 cc_library {277 name: "libvndk_sp_private",278 vendor_available: false,279 vndk: {280 enabled: true,281 support_system_process: true,282 },283 nocrt: true,284 }285 `)286 checkVndkModule(t, ctx, "libvndk", "vndk-VER", false, "")287 checkVndkModule(t, ctx, "libvndk_private", "vndk-VER", false, "")288 checkVndkModule(t, ctx, "libvndk_sp", "vndk-sp-VER", true, "")289 checkVndkModule(t, ctx, "libvndk_sp_private", "vndk-sp-VER", true, "")290}291func TestVndkDepError(t *testing.T) {292 // Check whether an error is emitted when a VNDK lib depends on a system lib.293 testCcError(t, "dependency \".*\" of \".*\" missing variant", `294 cc_library {295 name: "libvndk",296 vendor_available: true,297 vndk: {298 enabled: true,299 },300 shared_libs: ["libfwk"], // Cause error301 nocrt: true,302 }303 cc_library {304 name: "libfwk",305 nocrt: true,306 }307 `)308 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.309 testCcError(t, "dependency \".*\" of \".*\" missing variant", `310 cc_library {311 name: "libvndk",312 vendor_available: true,313 vndk: {314 enabled: true,315 },316 shared_libs: ["libvendor"], // Cause error317 nocrt: true,318 }319 cc_library {320 name: "libvendor",321 vendor: true,322 nocrt: true,323 }324 `)325 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.326 testCcError(t, "dependency \".*\" of \".*\" missing variant", `327 cc_library {328 name: "libvndk_sp",329 vendor_available: true,330 vndk: {331 enabled: true,332 support_system_process: true,333 },334 shared_libs: ["libfwk"], // Cause error335 nocrt: true,336 }337 cc_library {338 name: "libfwk",339 nocrt: true,340 }341 `)342 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.343 testCcError(t, "dependency \".*\" of \".*\" missing variant", `344 cc_library {345 name: "libvndk_sp",346 vendor_available: true,347 vndk: {348 enabled: true,349 support_system_process: true,350 },351 shared_libs: ["libvendor"], // Cause error352 nocrt: true,353 }354 cc_library {355 name: "libvendor",356 vendor: true,357 nocrt: true,358 }359 `)360 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.361 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `362 cc_library {363 name: "libvndk_sp",364 vendor_available: true,365 vndk: {366 enabled: true,367 support_system_process: true,368 },369 shared_libs: ["libvndk"], // Cause error370 nocrt: true,371 }372 cc_library {373 name: "libvndk",374 vendor_available: true,375 vndk: {376 enabled: true,377 },378 nocrt: true,379 }380 `)381 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.382 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `383 cc_library {384 name: "libvndk",385 vendor_available: true,386 vndk: {387 enabled: true,388 },389 shared_libs: ["libnonvndk"],390 nocrt: true,391 }392 cc_library {393 name: "libnonvndk",394 vendor_available: true,395 nocrt: true,396 }397 `)398 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.399 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `400 cc_library {401 name: "libvndkprivate",402 vendor_available: false,403 vndk: {404 enabled: true,405 },406 shared_libs: ["libnonvndk"],407 nocrt: true,408 }409 cc_library {410 name: "libnonvndk",411 vendor_available: true,412 nocrt: true,413 }414 `)415 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.416 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `417 cc_library {418 name: "libvndksp",419 vendor_available: true,420 vndk: {421 enabled: true,422 support_system_process: true,423 },424 shared_libs: ["libnonvndk"],425 nocrt: true,426 }427 cc_library {428 name: "libnonvndk",429 vendor_available: true,430 nocrt: true,431 }432 `)433 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.434 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `435 cc_library {436 name: "libvndkspprivate",437 vendor_available: false,438 vndk: {439 enabled: true,440 support_system_process: true,441 },442 shared_libs: ["libnonvndk"],443 nocrt: true,444 }445 cc_library {446 name: "libnonvndk",447 vendor_available: true,448 nocrt: true,449 }450 `)451}452func TestDoubleLoadbleDep(t *testing.T) {453 // okay to link : LLNDK -> double_loadable VNDK454 testCc(t, `455 cc_library {456 name: "libllndk",457 shared_libs: ["libdoubleloadable"],458 }459 llndk_library {460 name: "libllndk",461 symbol_file: "",462 }463 cc_library {464 name: "libdoubleloadable",465 vendor_available: true,466 vndk: {467 enabled: true,468 },469 double_loadable: true,470 }471 `)472 // okay to link : LLNDK -> VNDK-SP473 testCc(t, `474 cc_library {475 name: "libllndk",476 shared_libs: ["libvndksp"],477 }478 llndk_library {479 name: "libllndk",480 symbol_file: "",481 }482 cc_library {483 name: "libvndksp",484 vendor_available: true,485 vndk: {486 enabled: true,487 support_system_process: true,488 },489 }490 `)491 // okay to link : double_loadable -> double_loadable492 testCc(t, `493 cc_library {494 name: "libdoubleloadable1",495 shared_libs: ["libdoubleloadable2"],496 vendor_available: true,497 double_loadable: true,498 }499 cc_library {500 name: "libdoubleloadable2",501 vendor_available: true,502 double_loadable: true,503 }504 `)505 // okay to link : double_loadable VNDK -> double_loadable VNDK private506 testCc(t, `507 cc_library {508 name: "libdoubleloadable",509 vendor_available: true,510 vndk: {511 enabled: true,512 },513 double_loadable: true,514 shared_libs: ["libnondoubleloadable"],515 }516 cc_library {517 name: "libnondoubleloadable",518 vendor_available: false,519 vndk: {520 enabled: true,521 },522 double_loadable: true,523 }524 `)525 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable526 testCc(t, `527 cc_library {528 name: "libllndk",529 shared_libs: ["libcoreonly"],530 }531 llndk_library {532 name: "libllndk",533 symbol_file: "",534 }535 cc_library {536 name: "libcoreonly",537 shared_libs: ["libvendoravailable"],538 }539 // indirect dependency of LLNDK540 cc_library {541 name: "libvendoravailable",542 vendor_available: true,543 double_loadable: true,544 }545 `)546}547func TestDoubleLoadableDepError(t *testing.T) {548 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.549 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `550 cc_library {551 name: "libllndk",552 shared_libs: ["libnondoubleloadable"],553 }554 llndk_library {555 name: "libllndk",556 symbol_file: "",557 }558 cc_library {559 name: "libnondoubleloadable",560 vendor_available: true,561 vndk: {562 enabled: true,563 },564 }565 `)566 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.567 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `568 cc_library {569 name: "libllndk",570 no_libgcc: true,571 shared_libs: ["libnondoubleloadable"],572 }573 llndk_library {574 name: "libllndk",575 symbol_file: "",576 }577 cc_library {578 name: "libnondoubleloadable",579 vendor_available: true,580 }581 `)582 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.583 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `584 cc_library {585 name: "libdoubleloadable",586 vendor_available: true,587 double_loadable: true,588 shared_libs: ["libnondoubleloadable"],589 }590 cc_library {591 name: "libnondoubleloadable",592 vendor_available: true,593 }594 `)595 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.596 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `597 cc_library {598 name: "libdoubleloadable",599 vendor_available: true,600 double_loadable: true,601 shared_libs: ["libnondoubleloadable"],602 }603 cc_library {604 name: "libnondoubleloadable",605 vendor_available: true,606 vndk: {607 enabled: true,608 },609 }610 `)611 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.612 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `613 cc_library {614 name: "libdoubleloadable",615 vendor_available: true,616 vndk: {617 enabled: true,618 },619 double_loadable: true,620 shared_libs: ["libnondoubleloadable"],621 }622 cc_library {623 name: "libnondoubleloadable",624 vendor_available: false,625 vndk: {626 enabled: true,627 },628 }629 `)630 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.631 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `632 cc_library {633 name: "libllndk",634 shared_libs: ["libcoreonly"],635 }636 llndk_library {637 name: "libllndk",638 symbol_file: "",639 }640 cc_library {641 name: "libcoreonly",642 shared_libs: ["libvendoravailable"],643 }644 // indirect dependency of LLNDK645 cc_library {646 name: "libvendoravailable",647 vendor_available: true,648 }649 `)650}651func TestVndkMustNotBeProductSpecific(t *testing.T) {652 // Check whether an error is emitted when a vndk lib has 'product_specific: true'.653 testCcError(t, "product_specific must not be true when `vndk: {enabled: true}`", `654 cc_library {655 name: "libvndk",656 product_specific: true, // Cause error657 vendor_available: true,658 vndk: {659 enabled: true,660 },661 nocrt: true,662 }663 `)664}665func TestVndkExt(t *testing.T) {666 // This test checks the VNDK-Ext properties.667 ctx := testCc(t, `668 cc_library {669 name: "libvndk",670 vendor_available: true,671 vndk: {672 enabled: true,673 },674 nocrt: true,675 }676 cc_library {677 name: "libvndk_ext",678 vendor: true,679 vndk: {680 enabled: true,681 extends: "libvndk",682 },683 nocrt: true,684 }685 `)686 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk")687}688func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {689 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.690 ctx := testCcNoVndk(t, `691 cc_library {692 name: "libvndk",693 vendor_available: true,694 vndk: {695 enabled: true,696 },697 nocrt: true,698 }699 cc_library {700 name: "libvndk_ext",701 vendor: true,702 vndk: {703 enabled: true,704 extends: "libvndk",705 },706 nocrt: true,707 }708 `)709 // Ensures that the core variant of "libvndk_ext" can be found.710 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)711 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {712 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)713 }714}715func TestVndkExtError(t *testing.T) {716 // This test ensures an error is emitted in ill-formed vndk-ext definition.717 testCcError(t, "must set `vendor: true` to set `extends: \".*\"`", `718 cc_library {719 name: "libvndk",720 vendor_available: true,721 vndk: {722 enabled: true,723 },724 nocrt: true,725 }726 cc_library {727 name: "libvndk_ext",728 vndk: {729 enabled: true,730 extends: "libvndk",731 },732 nocrt: true,733 }734 `)735 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `736 cc_library {737 name: "libvndk",738 vendor_available: true,739 vndk: {740 enabled: true,741 },742 nocrt: true,743 }744 cc_library {745 name: "libvndk_ext",746 vendor: true,747 vndk: {748 enabled: true,749 },750 nocrt: true,751 }752 `)753}754func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {755 // This test ensures an error is emitted for inconsistent support_system_process.756 testCcError(t, "module \".*\" with mismatched support_system_process", `757 cc_library {758 name: "libvndk",759 vendor_available: true,760 vndk: {761 enabled: true,762 },763 nocrt: true,764 }765 cc_library {766 name: "libvndk_sp_ext",767 vendor: true,768 vndk: {769 enabled: true,770 extends: "libvndk",771 support_system_process: true,772 },773 nocrt: true,774 }775 `)776 testCcError(t, "module \".*\" with mismatched support_system_process", `777 cc_library {778 name: "libvndk_sp",779 vendor_available: true,780 vndk: {781 enabled: true,782 support_system_process: true,783 },784 nocrt: true,785 }786 cc_library {787 name: "libvndk_ext",788 vendor: true,789 vndk: {790 enabled: true,791 extends: "libvndk_sp",792 },793 nocrt: true,794 }795 `)796}797func TestVndkExtVendorAvailableFalseError(t *testing.T) {798 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library799 // with `vendor_available: false`.800 testCcError(t, "`extends` refers module \".*\" which does not have `vendor_available: true`", `801 cc_library {802 name: "libvndk",803 vendor_available: false,804 vndk: {805 enabled: true,806 },807 nocrt: true,808 }809 cc_library {810 name: "libvndk_ext",811 vendor: true,812 vndk: {813 enabled: true,814 extends: "libvndk",815 },816 nocrt: true,817 }818 `)819}820func TestVendorModuleUseVndkExt(t *testing.T) {821 // This test ensures a vendor module can depend on a VNDK-Ext library.822 testCc(t, `823 cc_library {824 name: "libvndk",825 vendor_available: true,826 vndk: {827 enabled: true,828 },829 nocrt: true,830 }831 cc_library {832 name: "libvndk_ext",833 vendor: true,834 vndk: {835 enabled: true,836 extends: "libvndk",837 },838 nocrt: true,839 }840 cc_library {841 name: "libvndk_sp",842 vendor_available: true,843 vndk: {844 enabled: true,845 support_system_process: true,846 },847 nocrt: true,848 }849 cc_library {850 name: "libvndk_sp_ext",851 vendor: true,852 vndk: {853 enabled: true,854 extends: "libvndk_sp",855 support_system_process: true,856 },857 nocrt: true,858 }859 cc_library {860 name: "libvendor",861 vendor: true,862 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],863 nocrt: true,864 }865 `)866}867func TestVndkExtUseVendorLib(t *testing.T) {868 // This test ensures a VNDK-Ext library can depend on a vendor library.869 testCc(t, `870 cc_library {871 name: "libvndk",872 vendor_available: true,873 vndk: {874 enabled: true,875 },876 nocrt: true,877 }878 cc_library {879 name: "libvndk_ext",880 vendor: true,881 vndk: {882 enabled: true,883 extends: "libvndk",884 },885 shared_libs: ["libvendor"],886 nocrt: true,887 }888 cc_library {889 name: "libvendor",890 vendor: true,891 nocrt: true,892 }893 `)894 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.895 testCc(t, `896 cc_library {897 name: "libvndk_sp",898 vendor_available: true,899 vndk: {900 enabled: true,901 support_system_process: true,902 },903 nocrt: true,904 }905 cc_library {906 name: "libvndk_sp_ext",907 vendor: true,908 vndk: {909 enabled: true,910 extends: "libvndk_sp",911 support_system_process: true,912 },913 shared_libs: ["libvendor"], // Cause an error914 nocrt: true,915 }916 cc_library {917 name: "libvendor",918 vendor: true,919 nocrt: true,920 }921 `)922}923func TestVndkSpExtUseVndkError(t *testing.T) {924 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK925 // library.926 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `927 cc_library {928 name: "libvndk",929 vendor_available: true,930 vndk: {931 enabled: true,932 },933 nocrt: true,934 }935 cc_library {936 name: "libvndk_sp",937 vendor_available: true,938 vndk: {939 enabled: true,940 support_system_process: true,941 },942 nocrt: true,943 }944 cc_library {945 name: "libvndk_sp_ext",946 vendor: true,947 vndk: {948 enabled: true,949 extends: "libvndk_sp",950 support_system_process: true,951 },952 shared_libs: ["libvndk"], // Cause an error953 nocrt: true,954 }955 `)956 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext957 // library.958 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `959 cc_library {960 name: "libvndk",961 vendor_available: true,962 vndk: {963 enabled: true,964 },965 nocrt: true,966 }967 cc_library {968 name: "libvndk_ext",969 vendor: true,970 vndk: {971 enabled: true,972 extends: "libvndk",973 },974 nocrt: true,975 }976 cc_library {977 name: "libvndk_sp",978 vendor_available: true,979 vndk: {980 enabled: true,981 support_system_process: true,982 },983 nocrt: true,984 }985 cc_library {986 name: "libvndk_sp_ext",987 vendor: true,988 vndk: {989 enabled: true,990 extends: "libvndk_sp",991 support_system_process: true,992 },993 shared_libs: ["libvndk_ext"], // Cause an error994 nocrt: true,995 }996 `)997}998func TestVndkUseVndkExtError(t *testing.T) {999 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a1000 // VNDK-Ext/VNDK-SP-Ext library.1001 testCcError(t, "dependency \".*\" of \".*\" missing variant", `1002 cc_library {1003 name: "libvndk",1004 vendor_available: true,1005 vndk: {1006 enabled: true,1007 },1008 nocrt: true,1009 }1010 cc_library {1011 name: "libvndk_ext",1012 vendor: true,1013 vndk: {1014 enabled: true,1015 extends: "libvndk",1016 },1017 nocrt: true,1018 }1019 cc_library {1020 name: "libvndk2",1021 vendor_available: true,1022 vndk: {1023 enabled: true,1024 },1025 shared_libs: ["libvndk_ext"],1026 nocrt: true,1027 }1028 `)1029 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `1030 cc_library {1031 name: "libvndk",1032 vendor_available: true,1033 vndk: {1034 enabled: true,1035 },1036 nocrt: true,1037 }1038 cc_library {1039 name: "libvndk_ext",1040 vendor: true,1041 vndk: {1042 enabled: true,1043 extends: "libvndk",1044 },1045 nocrt: true,1046 }1047 cc_library {1048 name: "libvndk2",1049 vendor_available: true,1050 vndk: {1051 enabled: true,1052 },1053 target: {1054 vendor: {1055 shared_libs: ["libvndk_ext"],1056 },1057 },1058 nocrt: true,1059 }1060 `)1061 testCcError(t, "dependency \".*\" of \".*\" missing variant", `1062 cc_library {1063 name: "libvndk_sp",1064 vendor_available: true,1065 vndk: {1066 enabled: true,1067 support_system_process: true,1068 },1069 nocrt: true,1070 }1071 cc_library {1072 name: "libvndk_sp_ext",1073 vendor: true,1074 vndk: {1075 enabled: true,1076 extends: "libvndk_sp",1077 support_system_process: true,1078 },1079 nocrt: true,1080 }1081 cc_library {1082 name: "libvndk_sp_2",1083 vendor_available: true,1084 vndk: {1085 enabled: true,1086 support_system_process: true,1087 },1088 shared_libs: ["libvndk_sp_ext"],1089 nocrt: true,1090 }1091 `)1092 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `1093 cc_library {1094 name: "libvndk_sp",1095 vendor_available: true,1096 vndk: {1097 enabled: true,1098 },1099 nocrt: true,1100 }1101 cc_library {1102 name: "libvndk_sp_ext",1103 vendor: true,1104 vndk: {1105 enabled: true,1106 extends: "libvndk_sp",1107 },1108 nocrt: true,1109 }1110 cc_library {1111 name: "libvndk_sp2",1112 vendor_available: true,1113 vndk: {1114 enabled: true,1115 },1116 target: {1117 vendor: {1118 shared_libs: ["libvndk_sp_ext"],1119 },1120 },1121 nocrt: true,1122 }1123 `)1124}1125var (1126 str11 = "01234567891"1127 str10 = str11[:10]1128 str9 = str11[:9]1129 str5 = str11[:5]1130 str4 = str11[:4]1131)1132var splitListForSizeTestCases = []struct {1133 in []string1134 out [][]string1135 size int1136}{1137 {1138 in: []string{str10},1139 out: [][]string{{str10}},1140 size: 10,1141 },1142 {1143 in: []string{str9},1144 out: [][]string{{str9}},1145 size: 10,1146 },1147 {1148 in: []string{str5},1149 out: [][]string{{str5}},1150 size: 10,1151 },1152 {1153 in: []string{str11},1154 out: nil,1155 size: 10,1156 },1157 {1158 in: []string{str10, str10},1159 out: [][]string{{str10}, {str10}},1160 size: 10,1161 },1162 {1163 in: []string{str9, str10},1164 out: [][]string{{str9}, {str10}},1165 size: 10,1166 },1167 {1168 in: []string{str10, str9},1169 out: [][]string{{str10}, {str9}},1170 size: 10,1171 },1172 {1173 in: []string{str5, str4},1174 out: [][]string{{str5, str4}},1175 size: 10,1176 },1177 {1178 in: []string{str5, str4, str5},1179 out: [][]string{{str5, str4}, {str5}},1180 size: 10,1181 },1182 {1183 in: []string{str5, str4, str5, str4},1184 out: [][]string{{str5, str4}, {str5, str4}},1185 size: 10,1186 },1187 {1188 in: []string{str5, str4, str5, str5},1189 out: [][]string{{str5, str4}, {str5}, {str5}},1190 size: 10,1191 },1192 {1193 in: []string{str5, str5, str5, str4},1194 out: [][]string{{str5}, {str5}, {str5, str4}},1195 size: 10,1196 },1197 {1198 in: []string{str9, str11},1199 out: nil,1200 size: 10,1201 },1202 {1203 in: []string{str11, str9},1204 out: nil,1205 size: 10,1206 },1207}1208func TestSplitListForSize(t *testing.T) {1209 for _, testCase := range splitListForSizeTestCases {1210 out, _ := splitListForSize(android.PathsForTesting(testCase.in...), testCase.size)1211 var outStrings [][]string1212 if len(out) > 0 {1213 outStrings = make([][]string, len(out))1214 for i, o := range out {1215 outStrings[i] = o.Strings()1216 }1217 }1218 if !reflect.DeepEqual(outStrings, testCase.out) {1219 t.Errorf("incorrect output:")1220 t.Errorf(" input: %#v", testCase.in)1221 t.Errorf(" size: %d", testCase.size)1222 t.Errorf(" expected: %#v", testCase.out)1223 t.Errorf(" got: %#v", outStrings)1224 }1225 }1226}1227var staticLinkDepOrderTestCases = []struct {1228 // This is a string representation of a map[moduleName][]moduleDependency .1229 // It models the dependencies declared in an Android.bp file.1230 inStatic string1231 // This is a string representation of a map[moduleName][]moduleDependency .1232 // It models the dependencies declared in an Android.bp file.1233 inShared string1234 // allOrdered is a string representation of a map[moduleName][]moduleDependency .1235 // The keys of allOrdered specify which modules we would like to check.1236 // The values of allOrdered specify the expected result (of the transitive closure of all1237 // dependencies) for each module to test1238 allOrdered string1239 // outOrdered is a string representation of a map[moduleName][]moduleDependency .1240 // The keys of outOrdered specify which modules we would like to check.1241 // The values of outOrdered specify the expected result (of the ordered linker command line)1242 // for each module to test.1243 outOrdered string1244}{1245 // Simple tests1246 {1247 inStatic: "",1248 outOrdered: "",1249 },1250 {1251 inStatic: "a:",1252 outOrdered: "a:",1253 },1254 {1255 inStatic: "a:b; b:",1256 outOrdered: "a:b; b:",1257 },1258 // Tests of reordering1259 {1260 // diamond example1261 inStatic: "a:d,b,c; b:d; c:d; d:",1262 outOrdered: "a:b,c,d; b:d; c:d; d:",1263 },1264 {1265 // somewhat real example1266 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",1267 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",1268 },1269 {1270 // multiple reorderings1271 inStatic: "a:b,c,d,e; d:b; e:c",1272 outOrdered: "a:d,b,e,c; d:b; e:c",1273 },1274 {1275 // should reorder without adding new transitive dependencies1276 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",1277 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",1278 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",1279 },1280 {1281 // multiple levels of dependencies1282 inStatic: "a:b,c,d,e,f,g,h; f:b,c,d; b:c,d; c:d",1283 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",1284 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",1285 },1286 // shared dependencies1287 {1288 // Note that this test doesn't recurse, to minimize the amount of logic it tests.1289 // So, we don't actually have to check that a shared dependency of c will change the order1290 // of a library that depends statically on b and on c. We only need to check that if c has1291 // a shared dependency on b, that that shows up in allOrdered.1292 inShared: "c:b",1293 allOrdered: "c:b",1294 outOrdered: "c:",1295 },1296 {1297 // This test doesn't actually include any shared dependencies but it's a reminder of what1298 // the second phase of the above test would look like1299 inStatic: "a:b,c; c:b",1300 allOrdered: "a:c,b; c:b",1301 outOrdered: "a:c,b; c:b",1302 },1303 // tiebreakers for when two modules specifying different orderings and there is no dependency1304 // to dictate an order1305 {1306 // if the tie is between two modules at the end of a's deps, then a's order wins1307 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",1308 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",1309 },1310 {1311 // if the tie is between two modules at the start of a's deps, then c's order is used1312 inStatic: "a1:d,e,b1,c1; b1:d,e; c1:e,d; a2:d,e,b2,c2; b2:d,e; c2:d,e",1313 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",1314 },1315 // Tests involving duplicate dependencies1316 {1317 // simple duplicate1318 inStatic: "a:b,c,c,b",1319 outOrdered: "a:c,b",1320 },1321 {1322 // duplicates with reordering1323 inStatic: "a:b,c,d,c; c:b",1324 outOrdered: "a:d,c,b",1325 },1326 // Tests to confirm the nonexistence of infinite loops.1327 // These cases should never happen, so as long as the test terminates and the1328 // result is deterministic then that should be fine.1329 {1330 inStatic: "a:a",1331 outOrdered: "a:a",1332 },1333 {1334 inStatic: "a:b; b:c; c:a",1335 allOrdered: "a:b,c; b:c,a; c:a,b",1336 outOrdered: "a:b; b:c; c:a",1337 },1338 {1339 inStatic: "a:b,c; b:c,a; c:a,b",1340 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",1341 outOrdered: "a:c,b; b:a,c; c:b,a",1342 },1343}1344// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])1345func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {1346 // convert from "a:b,c; d:e" to "a:b,c;d:e"1347 strippedText := strings.Replace(text, " ", "", -1)1348 if len(strippedText) < 1 {1349 return []android.Path{}, make(map[android.Path][]android.Path, 0)1350 }1351 allDeps = make(map[android.Path][]android.Path, 0)1352 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]1353 moduleTexts := strings.Split(strippedText, ";")1354 outputForModuleName := func(moduleName string) android.Path {1355 return android.PathForTesting(moduleName)1356 }1357 for _, moduleText := range moduleTexts {1358 // convert from "a:b,c" to ["a", "b,c"]1359 components := strings.Split(moduleText, ":")1360 if len(components) != 2 {1361 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))1362 }1363 moduleName := components[0]1364 moduleOutput := outputForModuleName(moduleName)1365 modulesInOrder = append(modulesInOrder, moduleOutput)1366 depString := components[1]1367 // convert from "b,c" to ["b", "c"]1368 depNames := strings.Split(depString, ",")1369 if len(depString) < 1 {1370 depNames = []string{}1371 }1372 var deps []android.Path1373 for _, depName := range depNames {1374 deps = append(deps, outputForModuleName(depName))1375 }1376 allDeps[moduleOutput] = deps1377 }1378 return modulesInOrder, allDeps1379}1380func TestLinkReordering(t *testing.T) {1381 for _, testCase := range staticLinkDepOrderTestCases {1382 errs := []string{}1383 // parse testcase1384 _, givenTransitiveDeps := parseModuleDeps(testCase.inStatic)1385 expectedModuleNames, expectedTransitiveDeps := parseModuleDeps(testCase.outOrdered)1386 if testCase.allOrdered == "" {1387 // allow the test case to skip specifying allOrdered1388 testCase.allOrdered = testCase.outOrdered1389 }1390 _, expectedAllDeps := parseModuleDeps(testCase.allOrdered)1391 _, givenAllSharedDeps := parseModuleDeps(testCase.inShared)1392 // For each module whose post-reordered dependencies were specified, validate that1393 // reordering the inputs produces the expected outputs.1394 for _, moduleName := range expectedModuleNames {1395 moduleDeps := givenTransitiveDeps[moduleName]1396 givenSharedDeps := givenAllSharedDeps[moduleName]1397 orderedAllDeps, orderedDeclaredDeps := orderDeps(moduleDeps, givenSharedDeps, givenTransitiveDeps)1398 correctAllOrdered := expectedAllDeps[moduleName]1399 if !reflect.DeepEqual(orderedAllDeps, correctAllOrdered) {1400 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedAllDeps."+1401 "\nin static:%q"+1402 "\nin shared:%q"+1403 "\nmodule: %v"+1404 "\nexpected: %s"+1405 "\nactual: %s",1406 testCase.inStatic, testCase.inShared, moduleName, correctAllOrdered, orderedAllDeps))1407 }1408 correctOutputDeps := expectedTransitiveDeps[moduleName]1409 if !reflect.DeepEqual(correctOutputDeps, orderedDeclaredDeps) {1410 errs = append(errs, fmt.Sprintf("orderDeps returned incorrect orderedDeclaredDeps."+1411 "\nin static:%q"+1412 "\nin shared:%q"+1413 "\nmodule: %v"+1414 "\nexpected: %s"+1415 "\nactual: %s",1416 testCase.inStatic, testCase.inShared, moduleName, correctOutputDeps, orderedDeclaredDeps))1417 }1418 }1419 if len(errs) > 0 {1420 sort.Strings(errs)1421 for _, err := range errs {1422 t.Error(err)1423 }1424 }1425 }1426}1427func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {1428 for _, moduleName := range moduleNames {1429 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)1430 output := module.outputFile.Path()1431 paths = append(paths, output)1432 }1433 return paths1434}1435func TestStaticLibDepReordering(t *testing.T) {1436 ctx := testCc(t, `1437 cc_library {1438 name: "a",1439 static_libs: ["b", "c", "d"],1440 stl: "none",1441 }1442 cc_library {1443 name: "b",1444 stl: "none",1445 }1446 cc_library {1447 name: "c",1448 static_libs: ["b"],1449 stl: "none",1450 }1451 cc_library {1452 name: "d",1453 stl: "none",1454 }1455 `)1456 variant := "android_arm64_armv8-a_core_static"1457 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)1458 actual := moduleA.depsInLinkOrder1459 expected := getOutputPaths(ctx, variant, []string{"c", "b", "d"})1460 if !reflect.DeepEqual(actual, expected) {1461 t.Errorf("staticDeps orderings were not propagated correctly"+1462 "\nactual: %v"+1463 "\nexpected: %v",1464 actual,1465 expected,1466 )1467 }1468}1469func TestStaticLibDepReorderingWithShared(t *testing.T) {1470 ctx := testCc(t, `1471 cc_library {1472 name: "a",1473 static_libs: ["b", "c"],1474 stl: "none",1475 }1476 cc_library {1477 name: "b",1478 stl: "none",1479 }1480 cc_library {1481 name: "c",1482 shared_libs: ["b"],1483 stl: "none",1484 }1485 `)1486 variant := "android_arm64_armv8-a_core_static"1487 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)1488 actual := moduleA.depsInLinkOrder1489 expected := getOutputPaths(ctx, variant, []string{"c", "b"})1490 if !reflect.DeepEqual(actual, expected) {1491 t.Errorf("staticDeps orderings did not account for shared libs"+1492 "\nactual: %v"+1493 "\nexpected: %v",1494 actual,1495 expected,1496 )1497 }1498}1499func TestLlndkHeaders(t *testing.T) {1500 ctx := testCc(t, `1501 llndk_headers {1502 name: "libllndk_headers",1503 export_include_dirs: ["my_include"],1504 }1505 llndk_library {1506 name: "libllndk",1507 export_llndk_headers: ["libllndk_headers"],1508 }1509 cc_library {1510 name: "libvendor",1511 shared_libs: ["libllndk"],1512 vendor: true,1513 srcs: ["foo.c"],1514 no_libgcc: true,1515 nocrt: true,1516 }1517 `)1518 // _static variant is used since _shared reuses *.o from the static variant1519 cc := ctx.ModuleForTests("libvendor", "android_arm_armv7-a-neon_vendor_static").Rule("cc")1520 cflags := cc.Args["cFlags"]1521 if !strings.Contains(cflags, "-Imy_include") {1522 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)1523 }1524}1525func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {1526 actual := module.Properties.AndroidMkRuntimeLibs1527 if !reflect.DeepEqual(actual, expected) {1528 t.Errorf("incorrect runtime_libs for shared libs"+1529 "\nactual: %v"+1530 "\nexpected: %v",1531 actual,1532 expected,1533 )1534 }1535}1536const runtimeLibAndroidBp = `1537 cc_library {1538 name: "libvendor_available1",1539 vendor_available: true,1540 no_libgcc : true,1541 nocrt : true,1542 system_shared_libs : [],1543 }1544 cc_library {1545 name: "libvendor_available2",1546 vendor_available: true,1547 runtime_libs: ["libvendor_available1"],1548 no_libgcc : true,1549 nocrt : true,1550 system_shared_libs : [],1551 }1552 cc_library {1553 name: "libvendor_available3",1554 vendor_available: true,1555 runtime_libs: ["libvendor_available1"],1556 target: {1557 vendor: {1558 exclude_runtime_libs: ["libvendor_available1"],1559 }1560 },1561 no_libgcc : true,1562 nocrt : true,1563 system_shared_libs : [],1564 }1565 cc_library {1566 name: "libcore",1567 runtime_libs: ["libvendor_available1"],1568 no_libgcc : true,1569 nocrt : true,1570 system_shared_libs : [],1571 }1572 cc_library {1573 name: "libvendor1",1574 vendor: true,1575 no_libgcc : true,1576 nocrt : true,1577 system_shared_libs : [],1578 }1579 cc_library {1580 name: "libvendor2",1581 vendor: true,1582 runtime_libs: ["libvendor_available1", "libvendor1"],1583 no_libgcc : true,1584 nocrt : true,1585 system_shared_libs : [],1586 }1587`1588func TestRuntimeLibs(t *testing.T) {1589 ctx := testCc(t, runtimeLibAndroidBp)1590 // runtime_libs for core variants use the module names without suffixes.1591 variant := "android_arm64_armv8-a_core_shared"1592 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)1593 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)1594 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)1595 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)1596 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core1597 // and vendor variants.1598 variant = "android_arm64_armv8-a_vendor_shared"1599 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)1600 checkRuntimeLibs(t, []string{"libvendor_available1.vendor"}, module)1601 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)1602 checkRuntimeLibs(t, []string{"libvendor_available1.vendor", "libvendor1"}, module)1603}1604func TestExcludeRuntimeLibs(t *testing.T) {1605 ctx := testCc(t, runtimeLibAndroidBp)1606 variant := "android_arm64_armv8-a_core_shared"1607 module := ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)1608 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)1609 variant = "android_arm64_armv8-a_vendor_shared"1610 module = ctx.ModuleForTests("libvendor_available3", variant).Module().(*Module)1611 checkRuntimeLibs(t, nil, module)1612}1613func TestRuntimeLibsNoVndk(t *testing.T) {1614 ctx := testCcNoVndk(t, runtimeLibAndroidBp)1615 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.1616 variant := "android_arm64_armv8-a_core_shared"1617 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)1618 checkRuntimeLibs(t, []string{"libvendor_available1"}, module)1619 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)1620 checkRuntimeLibs(t, []string{"libvendor_available1", "libvendor1"}, module)1621}1622func checkStaticLibs(t *testing.T, expected []string, module *Module) {1623 actual := module.Properties.AndroidMkStaticLibs1624 if !reflect.DeepEqual(actual, expected) {1625 t.Errorf("incorrect static_libs"+1626 "\nactual: %v"+1627 "\nexpected: %v",1628 actual,1629 expected,1630 )1631 }1632}1633const staticLibAndroidBp = `1634 cc_library {1635 name: "lib1",1636 }1637 cc_library {1638 name: "lib2",1639 static_libs: ["lib1"],1640 }1641`1642func TestStaticLibDepExport(t *testing.T) {1643 ctx := testCc(t, staticLibAndroidBp)1644 // Check the shared version of lib2.1645 variant := "android_arm64_armv8-a_core_shared"1646 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)1647 checkStaticLibs(t, []string{"lib1", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)1648 // Check the static version of lib2.1649 variant = "android_arm64_armv8-a_core_static"1650 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)1651 // libc++_static is linked additionally.1652 checkStaticLibs(t, []string{"lib1", "libc++_static", "libclang_rt.builtins-aarch64-android", "libatomic", "libgcc_stripped"}, module)1653}1654var compilerFlagsTestCases = []struct {1655 in string1656 out bool1657}{1658 {1659 in: "a",1660 out: false,1661 },1662 {1663 in: "-a",1664 out: true,1665 },1666 {1667 in: "-Ipath/to/something",1668 out: false,1669 },1670 {1671 in: "-isystempath/to/something",1672 out: false,1673 },1674 {1675 in: "--coverage",1676 out: false,1677 },1678 {1679 in: "-include a/b",1680 out: true,1681 },1682 {1683 in: "-include a/b c/d",1684 out: false,1685 },1686 {1687 in: "-DMACRO",1688 out: true,1689 },1690 {1691 in: "-DMAC RO",1692 out: false,1693 },1694 {1695 in: "-a -b",1696 out: false,1697 },1698 {1699 in: "-DMACRO=definition",1700 out: true,1701 },1702 {1703 in: "-DMACRO=defi nition",1704 out: true, // TODO(jiyong): this should be false1705 },1706 {1707 in: "-DMACRO(x)=x + 1",1708 out: true,1709 },1710 {1711 in: "-DMACRO=\"defi nition\"",1712 out: true,1713 },1714}1715type mockContext struct {1716 BaseModuleContext1717 result bool1718}1719func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {1720 // CheckBadCompilerFlags calls this function when the flag should be rejected1721 ctx.result = false1722}1723func TestCompilerFlags(t *testing.T) {1724 for _, testCase := range compilerFlagsTestCases {1725 ctx := &mockContext{result: true}1726 CheckBadCompilerFlags(ctx, "", []string{testCase.in})1727 if ctx.result != testCase.out {1728 t.Errorf("incorrect output:")1729 t.Errorf(" input: %#v", testCase.in)1730 t.Errorf(" expected: %#v", testCase.out)1731 t.Errorf(" got: %#v", ctx.result)1732 }1733 }1734}1735func TestVendorPublicLibraries(t *testing.T) {1736 ctx := testCc(t, `1737 cc_library_headers {1738 name: "libvendorpublic_headers",1739 export_include_dirs: ["my_include"],1740 }...

Full Screen

Full Screen

rtt.go

Source:rtt.go Github

copy

Full Screen

...77}78// serviceNodeSorter takes a list of health checks and a parallel vector of79// distances and implements sort.Interface, keeping both structures coherent and80// sorting by distance.81type healthCheckSorter struct {82 Checks structs.HealthChecks83 Vec []float6484}85// newHealthCheckSorter returns a new sorter for the given source coordinate and86// set of health checks with nodes.87func (s *Server) newHealthCheckSorter(cs lib.CoordinateSet, checks structs.HealthChecks) (sort.Interface, error) {88 state := s.fsm.State()89 vec := make([]float64, len(checks))90 for i, check := range checks {91 _, other, err := state.Coordinate(check.Node, nil)92 if err != nil {93 return nil, err94 }95 c1, c2 := cs.Intersect(other)96 vec[i] = lib.ComputeDistance(c1, c2)97 }98 return &healthCheckSorter{checks, vec}, nil99}100// See sort.Interface.101func (n *healthCheckSorter) Len() int {102 return len(n.Checks)103}104// See sort.Interface.105func (n *healthCheckSorter) Swap(i, j int) {106 n.Checks[i], n.Checks[j] = n.Checks[j], n.Checks[i]107 n.Vec[i], n.Vec[j] = n.Vec[j], n.Vec[i]108}109// See sort.Interface.110func (n *healthCheckSorter) Less(i, j int) bool {111 return n.Vec[i] < n.Vec[j]112}113// checkServiceNodeSorter takes a list of service nodes and a parallel vector of114// distances and implements sort.Interface, keeping both structures coherent and115// sorting by distance.116type checkServiceNodeSorter struct {117 Nodes structs.CheckServiceNodes118 Vec []float64119}120// newCheckServiceNodeSorter returns a new sorter for the given source coordinate121// and set of nodes with health checks.122func (s *Server) newCheckServiceNodeSorter(cs lib.CoordinateSet, nodes structs.CheckServiceNodes) (sort.Interface, error) {123 state := s.fsm.State()124 vec := make([]float64, len(nodes))125 for i, node := range nodes {126 _, other, err := state.Coordinate(node.Node.Node, nil)127 if err != nil {128 return nil, err129 }130 c1, c2 := cs.Intersect(other)131 vec[i] = lib.ComputeDistance(c1, c2)132 }133 return &checkServiceNodeSorter{nodes, vec}, nil134}135// See sort.Interface.136func (n *checkServiceNodeSorter) Len() int {137 return len(n.Nodes)138}139// See sort.Interface.140func (n *checkServiceNodeSorter) Swap(i, j int) {141 n.Nodes[i], n.Nodes[j] = n.Nodes[j], n.Nodes[i]142 n.Vec[i], n.Vec[j] = n.Vec[j], n.Vec[i]143}144// See sort.Interface.145func (n *checkServiceNodeSorter) Less(i, j int) bool {146 return n.Vec[i] < n.Vec[j]147}148// newSorterByDistanceFrom returns a sorter for the given type.149func (s *Server) newSorterByDistanceFrom(cs lib.CoordinateSet, subj interface{}) (sort.Interface, error) {150 switch v := subj.(type) {151 case structs.Nodes:152 return s.newNodeSorter(cs, v)153 case structs.ServiceNodes:154 return s.newServiceNodeSorter(cs, v)155 case structs.HealthChecks:156 return s.newHealthCheckSorter(cs, v)157 case structs.CheckServiceNodes:158 return s.newCheckServiceNodeSorter(cs, v)159 default:160 panic(fmt.Errorf("Unhandled type passed to newSorterByDistanceFrom: %#v", subj))161 }162}163// sortNodesByDistanceFrom is used to sort results from our service catalog based164// on the round trip time from the given source node. Nodes with missing coordinates165// will get stable sorted at the end of the list.166//167// If coordinates are disabled this will be a no-op.168func (s *Server) sortNodesByDistanceFrom(source structs.QuerySource, subj interface{}) error {169 // We can't sort if there's no source node.170 if source.Node == "" {171 return nil172 }...

Full Screen

Full Screen

codelib.go

Source:codelib.go Github

copy

Full Screen

...12 Class = "CODELIB"13)14// Get the first argument, "self", and cast it from userdata to a library (which is really a key/value).15func checkLibrary(L *lua.LState) pinterface.IKeyValue {16 ud := L.CheckUserData(1)17 if hash, ok := ud.Value.(pinterface.IKeyValue); ok {18 return hash19 }20 L.ArgError(1, "code library expected")21 return nil22}23// Given a namespace, register Lua code.24// Takes two strings, returns true if successful.25func libAdd(L *lua.LState) int {26 lualib := checkLibrary(L) // arg 127 namespace := L.ToString(2)28 if namespace == "" {29 L.ArgError(2, "namespace expected")30 }31 code := L.ToString(3)32 if code == "" {33 log.Warn("Empty Lua code given to codelib:add")34 L.Push(lua.LBool(false))35 return 136 }37 // Append the new code to the old code, if any38 oldcode, err := lualib.Get(namespace)39 if err != nil {40 oldcode = ""41 } else {42 oldcode += "\n"43 }44 L.Push(lua.LBool(nil == lualib.Set(namespace, oldcode+code)))45 return 1 // number of results46}47// Given a namespace, register Lua code as the only code.48// Takes two strings, returns true if successful.49func libSet(L *lua.LState) int {50 lualib := checkLibrary(L) // arg 151 namespace := L.ToString(2)52 if namespace == "" {53 L.ArgError(2, "namespace expected")54 }55 code := L.ToString(3)56 // Empty string is fine, for clearing a key57 //if code == "" {58 // log.Warn("Empty Lua code given to codelib:set")59 // L.Push(lua.LBool(false))60 // return 161 //}62 L.Push(lua.LBool(nil == lualib.Set(namespace, code)))63 return 1 // number of results64}65// Given a namespace, return Lua code, or an empty string.66func libGet(L *lua.LState) int {67 lualib := checkLibrary(L) // arg 168 namespace := L.ToString(2)69 if namespace == "" {70 L.ArgError(2, "namespace expected")71 }72 // Retrieve the Lua code from the HashMap73 code, err := lualib.Get(namespace)74 if err != nil {75 // Return an empty string if there was an error76 L.Push(lua.LString(""))77 }78 // Return the requested Lua code79 L.Push(lua.LString(code))80 return 1 // number of results81}82// Given a namespace, fetch all registered Lua code as a string.83// Then run the Lua code for this LState.84// Returns true of successful.85func libImport(L *lua.LState) int {86 lualib := checkLibrary(L) // arg 187 namespace := L.ToString(2)88 if namespace == "" {89 L.ArgError(2, "namespace expected")90 }91 code, err := lualib.Get(namespace)92 if err != nil {93 // Return false if there was an error94 L.Push(lua.LBool(false)) // error95 return 196 }97 if err := L.DoString(code); err != nil {98 L.Close()99 log.Errorf("Error when importing Lua code:\n%s", err)100 L.Push(lua.LBool(false)) // error101 return 1 // number of results102 }103 L.Push(lua.LBool(true)) // ok104 return 1 // number of results105}106// String representation107func libToString(L *lua.LState) int {108 L.Push(lua.LString("Code Library"))109 return 1 // number of results110}111// Clear the current code library112func libClear(L *lua.LState) int {113 lualib := checkLibrary(L) // arg 1114 L.Push(lua.LBool(nil == lualib.Remove()))115 return 1 // number of results116}117// Create a new code library.118// id is the name of the hash map.119func newCodeLibrary(L *lua.LState, creator pinterface.ICreator, id string) (*lua.LUserData, error) {120 // Create a new Lua Library (hash map)121 lualib, err := creator.NewKeyValue(id)122 if err != nil {123 return nil, err124 }125 // Create a new userdata struct126 ud := L.NewUserData()127 ud.Value = lualib128 L.SetMetatable(ud, L.GetTypeMetatable(Class))129 return ud, nil130}131// The hash map methods that are to be registered132var libMethods = map[string]lua.LGFunction{133 "__tostring": libToString,134 "add": libAdd,135 "set": libSet,136 "get": libGet,137 "import": libImport,138 "clear": libClear,139}140// Load makes functions related to building a library of Lua code available141func Load(L *lua.LState, creator pinterface.ICreator) {142 // Register the Library class and the methods that belongs with it.143 mt := L.NewTypeMetatable(Class)144 mt.RawSetH(lua.LString("__index"), mt)145 L.SetFuncs(mt, libMethods)146 // The constructor for new Libraries takes only an optional id147 L.SetGlobal("CodeLib", L.NewFunction(func(L *lua.LState) int {148 // Check if the optional argument is given149 id := defaultID150 if L.GetTop() == 1 {151 id = L.ToString(1)152 }153 // Create a new Library in Lua154 userdata, err := newCodeLibrary(L, creator, id)155 if err != nil {156 L.Push(lua.LNil)157 L.Push(lua.LString(err.Error()))158 L.Push(lua.LNumber(1))159 return 3 // Number of returned values160 }161 // Return the hash map object162 L.Push(userdata)...

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib.Check()4}5import (6func Check() {7 fmt.Println("Check method of lib class")8}9import (10func Check() {11 fmt.Println("Check method of lib class")12}13import (14func main() {15 lib.Check()16}17import (18func Check() {19 fmt.Println("Check method of lib class")20}21import (22func main() {

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import "lib"2func main() {3 lib.Check()4}5import "fmt"6func Check() {7 fmt.Println("Check")8}

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Check())4}5func Check() bool {6}7 /usr/lib/go/src/pkg/lib (from $GOROOT)8 /home/username/go/src/lib (from $GOPATH)9@user2338897 I have set the GOROOT variable. But still I am not able to import the package. go env command is not working. It is giving the following error. go: cannot find GOROOT directory: /usr/lib/go

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Print("Enter two numbers : ")4 fmt.Scan(&a, &b)5 fmt.Println(lib.Check(a, b))6}

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Check())4}5func Check() string {6}

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib.Check()4}5import (6func Check() {7}8import (9func main() {10 fmt.Println("Hello, World!")11}12 /usr/lib/go-1.6/src/lib (from $GOROOT)13 /home/username/go/src/lib (from $GOPATH)14import (15func Print() {16 fmt.Println("Hello, World!")17}18import (19func TestPrint(t *testing.T) {20 lib.Print()21}

Full Screen

Full Screen

Check

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World!")4 lib.Check()5}6import (7func Check() {8 fmt.Println("check method")9}10import "testing"11func TestCheck(t *testing.T) {12 Check()13}14I am trying to run a test on a package that imports a package that import

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful