File: test_assignment.R

package info (click to toggle)
r-cran-bigmemory 4.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 648 kB
  • sloc: cpp: 4,930; ansic: 131; sh: 13; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
context("big.matrix assignment")

set.seed(123)
z <- filebacked.big.matrix(3, 3, type='double', init=123.0,
                           backingfile="example.bin",
                           descriptorfile="example.desc",
                           dimnames=list(c('a','b','c'), c('d', 'e', 'f')))

mat <- matrix(1:9, ncol = 3, nrow = 3, dimnames = list(letters[1:3], 
                                                       LETTERS[1:3]))
bm <- as.big.matrix(mat, type = "double")
z[] <- mat

test_that("Able to assign individual elements", {
  
  tmp <- bm[]
  
  # element assign
  bm[1,3] <- 15.123
  z[1,3] <- 15.123
  mat[1,3] <- 15.123
  
  expect_equivalent(bm[1,3], 15.123)
  expect_equivalent(z[1,3], 15.123)
  
  bm[] <- tmp
  z[] <- tmp
})

test_that("Able to assign non-contiguous individual elements", {
  
  tmp <- bm[]
  
  # element assign
  bm[c(1,3,5)] <- 15.123
  z[c(1,3,5)] <- 15.123
  mat[c(1,3,5)] <- 15.123
  
  expect_equivalent(bm[], mat)
  expect_equivalent(z[], mat)
  
  bm[] <- tmp
  z[] <- tmp
})

test_that("Able to assign rowwise elements", {
  
  row <- rnorm(3)
  tmp <- bm[]
  
  # row assign
  bm[2,] <- row
  z[2,] <- row
  mat[2,] <- row
  
  expect_equivalent(bm[], mat)
  expect_equivalent(z[], mat)
  
  bm[] <- tmp
  z[] <- tmp
})

test_that("Able to assign columnwise elements", {
  
  col <- rnorm(3)
  tmp <- bm[]
  
  # column assign
  bm[,3] <- col
  z[,3] <- col
  mat[,3] <- col
  
  expect_equivalent(bm[], mat)
  expect_equivalent(z[], mat)
  
  bm[] <- tmp
  z[] <- tmp
})


test_that("Able to assign non-contiguous columns", {
  
  tmp <- bm[]
  
  # column assign
  bm[,c(1,3)] <- mat[,c(1,3)]
  z[,c(1,3)] <- mat[,c(1,3)]
  mat[,c(1,3)] <- mat[,c(1,3)]
  
  expect_equivalent(bm[], mat)
  expect_equivalent(z[], mat)
  
  bm[] <- tmp
  z[] <- tmp
})

test_that("Able to assign matrix to rows", {
  tmp <- bm[]
  
  newmat <- matrix(1:6, 2, 3)
  
  bm[1:2,] <- newmat[1:2,]
  z[1:2,] <- newmat[1:2,]
  mat[1:2,] <- newmat[1:2,]
  
  expect_equivalent(bm[], mat)
  expect_equivalent(z[], mat)
  
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
  
})

test_that("Able to assign row-wise using logical vectors", {
  tmp <- bm[]
  bm[c(TRUE,FALSE,TRUE),] <- 123
  z[c(TRUE,FALSE,TRUE),] <- 123
  mat[c(TRUE,FALSE,TRUE),] <- 123
  expect_equivalent (bm[], mat)
  expect_equivalent (z[], mat)
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
})

test_that("Able to assign column-wise using logical vectors", {
  tmp <- bm[]
  bm[,c(TRUE,FALSE,TRUE)] <- 123
  z[,c(TRUE,FALSE,TRUE)] <- 123
  mat[,c(TRUE,FALSE,TRUE)] <- 123
  expect_equivalent (bm[], mat)
  expect_equivalent (z[], mat)
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
})

test_that("Able to assign logical vectors for column and vector indices", {
  tmp <- bm[]
  bm[c(TRUE,FALSE,FALSE),c(TRUE,FALSE,TRUE)] <- 123
  z[c(TRUE,FALSE,FALSE),c(TRUE,FALSE,TRUE)] <- 123
  mat[c(TRUE,FALSE,FALSE),c(TRUE,FALSE,TRUE)] <- 123
  expect_equivalent (bm[], mat)
  expect_equivalent (z[], mat)
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
})

test_that("Able to assign row-wise using logical vectors and numeric", {
  tmp <- bm[]
  bm[c(TRUE,FALSE,TRUE),1:2] <- 123
  z[c(TRUE,FALSE,TRUE),1:2] <- 123
  mat[c(TRUE,FALSE,TRUE),1:2] <- 123
  expect_equivalent (bm[], mat)
  expect_equivalent (z[], mat)
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
})

test_that("Able to assign column-wise using logical vectors and numeric", {
  tmp <- bm[]
  bm[1:2,c(TRUE,FALSE,TRUE)] <- 123
  z[1:2,c(TRUE,FALSE,TRUE)] <- 123
  mat[1:2,c(TRUE,FALSE,TRUE)] <- 123
  expect_equivalent (bm[], mat)
  expect_equivalent (z[], mat)
  bm[] <- tmp
  z[] <- tmp
  mat <- tmp
})

test_that("Able to assign based on row names and column names", {
  z[c('a', 'b'), c('d', 'e')] <- 1
})

test_that("Able to assign based on row names", {
  z[c('a', 'b'),] <- 1
})

test_that("Able to assign based on column names", {
  z[, c('d', 'e')] <- 1
})

test_that("Able to assign based on logical rows and column names", {
  z[c(TRUE, TRUE, FALSE), c('d', 'e')] <- 1
})

test_that("Able to assign based on integer rows and column names", {
  z[2:3, c('d', 'e')] <- 1
})


rm(z)
gc()
file.remove('example.bin')
file.remove('example.desc')