@@ -619,6 +619,10 @@ stopifnot(identical(names(x), c("1", "3")))
619619
620620
621621# # Expand and shrink
622+ x <- listenv(a = 1 , b = 2 )
623+ length(x ) <- 2L
624+ stopifnot(length(x ) == 2L )
625+
622626x <- listenv()
623627stopifnot(length(x ) == 0L )
624628length(x ) <- 3L
@@ -780,6 +784,197 @@ res <- try(x[[""]] <- 1, silent = TRUE)
780784stopifnot(inherits(res , " try-error" ))
781785
782786
787+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
788+ # # print() - various cases
789+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
790+ # # print with 1 element
791+ x <- listenv(a = 1 )
792+ print(x )
793+
794+ # # print with no named elements
795+ x <- listenv()
796+ names(x ) <- character (0L )
797+ print(x )
798+
799+ # # print matrix with all dimnames
800+ x <- as.listenv(1 : 6 )
801+ dim(x ) <- c(2 , 3 )
802+ dimnames(x ) <- list (c(" r1" , " r2" ), c(" c1" , " c2" , " c3" ))
803+ print(x )
804+
805+ # # print matrix with only row names
806+ x <- as.listenv(1 : 6 )
807+ dim(x ) <- c(2 , 3 )
808+ dimnames(x ) <- list (c(" r1" , " r2" ), NULL )
809+ print(x )
810+
811+ # # print matrix with only column names
812+ x <- as.listenv(1 : 6 )
813+ dim(x ) <- c(2 , 3 )
814+ dimnames(x ) <- list (NULL , c(" c1" , " c2" , " c3" ))
815+ print(x )
816+
817+ # # print matrix with NULL dimnames
818+ x <- as.listenv(1 : 6 )
819+ dim(x ) <- c(2 , 3 )
820+ dimnames(x ) <- list (NULL , NULL )
821+ print(x )
822+
823+ # # print matrix with no dimnames
824+ x <- as.listenv(1 : 6 )
825+ dim(x ) <- c(2 , 3 )
826+ print(x )
827+
828+ # # print 3d-array with all dimnames
829+ x <- as.listenv(1 : 24 )
830+ dim(x ) <- c(2 , 3 , 4 )
831+ dimnames(x ) <- list (letters [1 : 2 ], letters [1 : 3 ], letters [1 : 4 ])
832+ print(x )
833+
834+ # # print 3d-array with partial dimnames
835+ x <- as.listenv(1 : 24 )
836+ dim(x ) <- c(2 , 3 , 4 )
837+ dimnames(x ) <- list (letters [1 : 2 ], NULL , letters [1 : 4 ])
838+ print(x )
839+
840+ # # print 3d-array with no dimnames
841+ x <- as.listenv(1 : 24 )
842+ dim(x ) <- c(2 , 3 , 4 )
843+ print(x )
844+
845+ # # print 3d-array with NULL dimnames
846+ x <- as.listenv(1 : 24 )
847+ dim(x ) <- c(2 , 3 , 4 )
848+ dimnames(x ) <- list (NULL , NULL , NULL )
849+ print(x )
850+
851+
852+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
853+ # # map() is defunct
854+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
855+ x <- listenv(a = 1 , b = 2 )
856+ res <- try(map(x ), silent = TRUE )
857+ stopifnot(inherits(res , " try-error" ))
858+
859+
860+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
861+ # # Additional exception handling for assign/remove helpers
862+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
863+ x <- listenv()
864+ length(x ) <- 3L
865+ names(x ) <- c(" a" , " b" , " c" )
866+
867+ # # remove_by_name: non-existing name
868+ x $ nonexistent <- NULL
869+
870+ # # remove_by_index: out of range (no-op)
871+ x [[10L ]] <- NULL
872+
873+ # # [<- with zero-length replacement value
874+ res <- try({ x [1 : 2 ] <- list () }, silent = TRUE )
875+ stopifnot(inherits(res , " try-error" ))
876+
877+
878+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
879+ # # all.equal.listenv - identical objects
880+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
881+ x <- listenv(a = 1 , b = 2 )
882+ stopifnot(isTRUE(all.equal(x , x )))
883+
884+
885+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
886+ # # [[ with non-existing character name returns NULL
887+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
888+ x <- listenv(a = 1 , b = 2 )
889+ stopifnot(is.null(x [[" nonexistent" ]]))
890+
891+ # # [[ on unassigned (NA placeholder) element returns NULL
892+ x <- listenv()
893+ length(x ) <- 3L
894+ stopifnot(is.null(x [[2 ]]))
895+
896+
897+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
898+ # # [.listenv and [<-.listenv dimension mismatch
899+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
900+ x <- as.listenv(1 : 6 )
901+ dim(x ) <- c(2 , 3 )
902+
903+ # # [.listenv: wrong number of dimensions
904+ res <- try(x [1 , 2 , 3 ], silent = TRUE )
905+ stopifnot(inherits(res , " try-error" ))
906+
907+ # # [<-.listenv: wrong number of dimensions
908+ res <- try(x [1 , 2 , 3 ] <- 1 , silent = TRUE )
909+ stopifnot(inherits(res , " try-error" ))
910+
911+ # # [<-.listenv: multi-dim NULL with wrong number of non-missing dims
912+ res <- try(x [, ] <- NULL , silent = TRUE )
913+ stopifnot(inherits(res , " try-error" ))
914+
915+ # # [.listenv: mixed negative and positive subscripts
916+ res <- try(x [c(- 1 , 1 )], silent = TRUE )
917+ stopifnot(inherits(res , " try-error" ))
918+
919+
920+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
921+ # # as.listenv.environment
922+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
923+ e <- new.env(parent = emptyenv())
924+ e $ x <- 1
925+ e $ y <- 2
926+ x <- as.listenv(e )
927+ stopifnot(length(x ) == 2 )
928+ y <- as.list(x )
929+ stopifnot(all(sort(names(y )) == c(" x" , " y" )))
930+
931+
932+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
933+ # # [[ with non-existing name returns NULL
934+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
935+ x <- listenv(a = 1 , b = 2 )
936+ stopifnot(is.null(x [[" nonexistent" ]]))
937+
938+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
939+ # # Negative length error
940+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
941+ x <- listenv()
942+ res <- try(length(x ) <- - 1 , silent = TRUE )
943+ stopifnot(inherits(res , " try-error" ))
944+
945+
946+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
947+ # # map() is defunct
948+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
949+ x <- listenv(a = 1 )
950+ res <- try(listenv :: map(x ), silent = TRUE )
951+ stopifnot(inherits(res , " try-error" ))
952+
953+ # # Test deprecated path of map()
954+ Sys.setenv(R_LISTENV_MAP_DEPRECATED = " deprecated" )
955+ res <- withCallingHandlers(listenv :: map(x ), warning = function (w ) {
956+ invokeRestart(" muffleWarning" )
957+ })
958+ stopifnot(is.character(res ))
959+ Sys.unsetenv(" R_LISTENV_MAP_DEPRECATED" )
960+
961+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
962+ # # Mixing positive and negative subscripts in [
963+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
964+ x <- listenv()
965+ x [1 : 3 ] <- 1 : 3
966+ res <- try(x [c(1 , - 1 )], silent = TRUE )
967+ stopifnot(inherits(res , " try-error" ))
968+
969+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
970+ # # [<- with zero-length replacement
971+ # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
972+ x <- listenv()
973+ x [1 : 3 ] <- 1 : 3
974+ res <- try(x [1 ] <- list (), silent = TRUE )
975+ stopifnot(inherits(res , " try-error" ))
976+
977+
783978# # Cleanup
784979options(oopts )
785980rm(list = setdiff(ls(envir = globalenv()), ovars ), envir = globalenv())
0 commit comments