@@ -72,3 +72,161 @@ test_that("nClass replacing default constructor works", {
7272# rm(obj)
7373# gc()
7474})
75+
76+ test_that(" manual initialize works and Cpp ctor call is inserted" , {
77+ nc <- nClass(
78+ classname = " methods_test" ,
79+ Rpublic = list (
80+ Ra = 0 ,
81+ initialize = function () {
82+ print(" calling initialize" )
83+ self $ Ra <- 1
84+ },
85+ get_Ra = function () {
86+ self $ Ra
87+ },
88+ get_Ca = function () {
89+ self $ Ca
90+ }
91+ ),
92+ Cpublic = list (
93+ Ca = ' numericScalar' ,
94+ methods_test = nFunction(
95+ function () {
96+ nCpp(' Rprintf("calling c++ constructor\\ n")' )
97+ Ca <- 2
98+ },
99+ compileInfo = list (constructor = TRUE )
100+ )
101+ )
102+ )
103+
104+ obj <- nc $ new()
105+ expect_equal(obj $ Ra , 1 )
106+ expect_equal(obj $ get_Ra(), 1 )
107+ # obj$Ca
108+ # obj$get_Ca()
109+ # Need initialization of uncompiled Cpublic variables?
110+
111+ Cnc <- nCompile(nc )
112+ out <- capture_output(Cobj <- Cnc $ new())
113+ # the C++ initializer output should appear BEFORE the R initializer msg
114+ expect_true(regexpr(" initialize" , out ) > regexpr(" constructor" , out ))
115+
116+ expect_equal(Cobj $ Ra , 1 )
117+ expect_equal(Cobj $ get_Ra(), 1 )
118+ expect_equal(Cobj $ Ca , 2 )
119+ expect_equal(Cobj $ get_Ca(), 2 )
120+ rm(Cobj ); gc()
121+ })
122+
123+
124+ test_that(" manual initialize with hand-coded C++ initialization works" , {
125+ nc <- nClass(
126+ classname = " methods_test" ,
127+ Rpublic = list (
128+ Ra = 0 ,
129+ initialize = function () {
130+ print(" calling initialize" )
131+ if (isCompiled()) initializeCpp()
132+ self $ Ra <- 1
133+ },
134+ get_Ra = function () {
135+ self $ Ra
136+ },
137+ get_Ca = function () {
138+ self $ Ca
139+ }
140+ ),
141+ Cpublic = list (
142+ Ca = ' numericScalar' ,
143+ methods_test = nFunction(
144+ function () {
145+ nCpp(' Rprintf("calling c++ constructor\\ n")' )
146+ Ca <- 2
147+ },
148+ compileInfo = list (constructor = TRUE )
149+ )
150+ ),
151+ compileInfo = list (omit_automatic_Cpp_construction = TRUE )
152+ )
153+
154+ obj <- nc $ new()
155+ expect_equal(obj $ Ra , 1 )
156+ expect_equal(obj $ get_Ra(), 1 )
157+ expect_true(isFALSE(obj $ isCompiled()))
158+ # obj$Ca
159+ # obj$get_Ca()
160+ # Need initialization of uncompiled Cpublic variables?
161+
162+ Cnc <- nCompile(nc )
163+ out <- capture_output(Cobj <- Cnc $ new())
164+ # the C++ initializer output should now appear AFTER the R initializer msg
165+ expect_true(regexpr(" initialize" , out ) < regexpr(" constructor" , out ))
166+ expect_true(isTRUE(Cobj $ isCompiled()))
167+ expect_equal(Cobj $ Ra , 1 )
168+ expect_equal(Cobj $ get_Ra(), 1 )
169+ expect_equal(Cobj $ Ca , 2 )
170+ expect_equal(Cobj $ get_Ca(), 2 )
171+ rm(Cobj ); gc()
172+ })
173+
174+
175+ test_that(" manual initialize OMITTED with hand-coded C++ initialization compiles but is correctly broken" , {
176+ nc <- nClass(
177+ classname = " methods_test" ,
178+ Rpublic = list (
179+ Ra = 0 ,
180+ initialize = function () {
181+ print(" calling initialize" )
182+ # if(isCompiled()) initializeCpp() # OMITTED!
183+ self $ Ra <- 1
184+ },
185+ get_Ra = function () {
186+ self $ Ra
187+ },
188+ get_Ca = function () {
189+ self $ Ca
190+ }
191+ ),
192+ Cpublic = list (
193+ Ca = ' numericScalar' ,
194+ methods_test = nFunction(
195+ function () {
196+ nCpp(' Rprintf("calling c++ constructor\\ n")' )
197+ Ca <- 2
198+ },
199+ compileInfo = list (constructor = TRUE )
200+ )
201+ ),
202+ compileInfo = list (omit_automatic_Cpp_construction = TRUE )
203+ )
204+
205+ obj <- nc $ new()
206+ expect_equal(obj $ Ra , 1 )
207+ expect_equal(obj $ get_Ra(), 1 )
208+ expect_true(isFALSE(obj $ isCompiled()))
209+ # obj$Ca
210+ # obj$get_Ca()
211+ # Need initialization of uncompiled Cpublic variables?
212+
213+ Cnc <- nCompile(nc )
214+ out <- capture_output(Cobj <- Cnc $ new())
215+ # the C++ initializer output should now appear AFTER the R initializer msg
216+ expect_true(regexpr(" constructor" , out )== - 1 )
217+ expect_true(isTRUE(Cobj $ isCompiled()))
218+ expect_equal(Cobj $ Ra , 1 )
219+ expect_equal(Cobj $ get_Ra(), 1 )
220+ expect_error(Cobj $ Ca )
221+ expect_error(Cobj $ get_Ca())
222+
223+ out2 <- capture_output(Cobj $ initializeCpp())
224+ expect_true(regexpr(" constructor" , out2 )> 0 )
225+ expect_true(isTRUE(Cobj $ isCompiled()))
226+ expect_equal(Cobj $ Ra , 1 )
227+ expect_equal(Cobj $ get_Ra(), 1 )
228+ expect_equal(Cobj $ Ca , 2 )
229+ expect_equal(Cobj $ get_Ca(), 2 )
230+
231+ rm(Cobj ); gc()
232+ })
0 commit comments