From 4b6f6e27a9924567bb5b9a9e1c6ee8223e5ccec1 Mon Sep 17 00:00:00 2001 From: Joshua Wu Date: Fri, 3 May 2024 00:43:40 -0700 Subject: [PATCH] Replace unnecessary calls to getNumericRounding() in tests.Rraw where possible (#6119) * changed certain calls to getnumericrounding for the purpose of storing old rounding * review suggestions --- inst/tests/tests.Rraw | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 60004688e..db10ef3df 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -1247,7 +1247,6 @@ test(388, DT[,{ans = score[1] # Test unique.data.table for numeric columns within tolerance, for consistency with # with unique.data.frame which does this using paste. -old_rounding = getNumericRounding() DT = data.table(a=tan(pi*(1/4 + 1:10)),b=42L) # tan(...) from example in ?all.equal. test(395, all.equal(DT$a, rep(1,10))) @@ -1255,7 +1254,7 @@ test(396, length(unique(DT$a))>1) # 10 unique values on all CRAN machines (as o # commenting these two as they give different results on os x and linux. # test(397.1, unique(DT), DT[duplicated(DT)]) # default, no rounding # test(398.1, duplicated(DT), c(FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE)) -setNumericRounding(2L) +old_rounding = setNumericRounding(2L) test(397.2, unique(DT), DT[1]) # before v1.7.2 unique would return all 10 rows. For stability within tolerance, data.table has its own modified numeric sort. test(398.2, duplicated(DT), c(FALSE,rep(TRUE,9))) setNumericRounding(old_rounding) @@ -4084,13 +4083,12 @@ test(1146.3, set(DF, i=NULL, j="bla", value=seq_len(nrow(DF))), error="set() on if (test_longdouble) { # e.g. not on CRAN's solaris-sparc 32bit, and not under valgrind which uses 53 instead of 64 longdouble.digits - old = getNumericRounding() set.seed(6) x = rnorm(1e4)*1e4 # first 1e4 reduced from 1e6 to save ram, #5517 x = c(x, 11969.235757385, 11969.235757322) # add back 2 numbers from the 1e6 sample whose order is changed in test 1147.3 ans = base::sort.list(x, method="shell") - setNumericRounding(0) + old = setNumericRounding(0) test(1147.1, ans, forderv(x)) setNumericRounding(1) test(1147.2, ans, forderv(x)) @@ -4385,10 +4383,9 @@ test(1194, DT[.(x),.N], 1L) # tests bmerge uses twiddle DT[3, val:=0.0275016249291963] setkey(DT, NULL) # val[3] and val[4] are now equal, within 2 byte rounding test(1195, DT[,.N,keyby=val], setkey(DT,val)[,.N,by=val]) -old_rounding = getNumericRounding() # default is 0 test(1196.1, DT[,.N,by=val]$N, INT(1,1,1,1)) test(1196.2, DT[.(x),.N], 1L) -setNumericRounding(2L) +old_rounding = setNumericRounding(2L) # default is 0 test(1197.1, DT[,.N,by=val]$N, INT(1,1,2)) test(1197.2, DT[.(x),.N], 2L) setNumericRounding(old_rounding) @@ -4790,11 +4787,10 @@ ans = NULL if (nfail > oldnfail) cat(seedInfo, "\n") # to reproduce ############### -old_rounding = getNumericRounding() # turning off tolerance for UPCs (> 11 s.f. stored in numeric), #342 DT <- data.table(upc = c(301426027592, 301426027593, 314775802939, 314775802940, 314775803490, 314775803491, 314775815510, 314775815511, 314933000171, 314933000172), year = 2006:2007) -setNumericRounding(2L) +old_rounding = setNumericRounding(2L) test(1253, DT[,.N,by=upc]$N, rep.int(2L,5L)) setNumericRounding(0) test(1254, DT[,.N,by=upc], data.table(upc=DT$upc, N=1L)) @@ -4882,9 +4878,8 @@ test(1268.22, dt[, c(as.list(c), lapply(.SD, mean)), by=a], ### FR #2722 tests end here ### # Wide range numeric and integer64, to test all bits -old_rounding = getNumericRounding() +old_rounding = setNumericRounding(0) x = sample( c(seq(-1e100, 1e100, length.out=1e3), c(seq(-1e-100,1e-100,length.out=1e3))) ) # 1e5 reduced to 1e3, #5517 -setNumericRounding(0) test(1269, forderv(x), base::order(x)) setNumericRounding(2) # not affected by rounding test(1270, forderv(x), base::order(x)) @@ -4920,7 +4915,7 @@ setNumericRounding(old_rounding) # rounding of milliseconds, workaround, TO DO: #485 # http://stackoverflow.com/questions/22356957/rounding-milliseconds-of-posixct-in-data-table-v1-9-2-ok-in-1-8-10 -old_rounding = getNumericRounding() +old_rounding = setNumericRounding(2) DT = data.table(timestamp=as.POSIXct( c("2013-01-01 17:51:00.707", "2013-01-01 17:51:59.996", @@ -4928,7 +4923,6 @@ DT = data.table(timestamp=as.POSIXct( "2013-01-01 17:54:23.901", "2013-01-01 17:54:23.913", "2013-01-01 17:54:23.914"))) -setNumericRounding(2) test(1279, duplicated(DT), rep(c(FALSE,TRUE), c(4,2))) setNumericRounding(1) test(1280, duplicated(DT), rep(FALSE, 6)) @@ -13137,8 +13131,7 @@ test(1942.02, DT[,sum(v),keyby=id1,verbose=TRUE], data.table(id1=c("D","A","C"), setindex(DT,id2) test(1942.03, DT[,sum(v),keyby=id2,verbose=TRUE], data.table(id2=INT(3,9,2), V1=INT(6,6,3), key="id2"), output="Finding groups using uniqlist on index 'id2") setindex(DT,id3) -oldnr = getNumericRounding() # TODO: return old -setNumericRounding(0) +oldnr = setNumericRounding(0) test(1942.04, DT[,sum(v),keyby=id3,verbose=TRUE], data.table(id3=c(3.4, 9.1, 3.3), V1=INT(4,7,4), key="id3"), output="Finding groups using uniqlist on index 'id3'") setNumericRounding(1) test(1942.05, DT[,sum(v),keyby=id3,verbose=TRUE], data.table(id3=c(3.4, 9.1, 3.3), V1=INT(4,7,4), key="id3"), output="Finding groups using uniqlist on index 'id3'")