The R Project SVN R

Rev

Rev 70274 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
52030 jmc 1
## test (non-conditional) explicit inheritance
2
setClass("xy", representation(x="numeric", y="numeric"))
3
 
4
setIs("xy", "complex",
5
      coerce = function(from) complex(real = from@x, imaginary = from@y),
6
      replace = function(from, value) {
7
          from@x <- Re(value)
8
          from@y <- Im(value)
9
          from
10
      })
11
 
12
set.seed(124)
13
x1 <- rnorm(10)
14
y1 <- rnorm(10)
15
cc <- complex(real = x1, imaginary=y1)
16
xyc <- new("xy", x = x1, y = y1)
17
stopifnot(identical(cc, as(xyc, "complex")))
18
as(xyc, "complex") <- cc * 1i
19
stopifnot(identical(xyc, new("xy", x = -y1, y = x1)))
20
 
21
setGeneric("size", function(x)standardGeneric("size"))
58136 jmc 22
## check that generic for size() was created w/o a default method
23
stopifnot(is(size, "standardGeneric"),
24
          is.null(selectMethod("size", "ANY",optional=TRUE)))
52030 jmc 25
 
26
setMethod("size", "vector", function(x)length(x))
27
 
28
## class "xy" should inherit the vector method through complex
29
stopifnot(identical(size(xyc), length(x1)))
30
removeClass("xy")
31
removeGeneric("size")
70274 maechler 32
 
33
 
34
### Related to  numeric <-> double <-> integer  proposals, end of 2015, on R-devel
35
myN   <- setClass("myN",   contains="numeric")
36
myNid <- setClass("myNid", contains="numeric", representation(id="character"))
37
NN <-    setClass("NN", representation(x="numeric"))
38
 
39
(m1 <- myN  (1:3))
40
(m2 <- myNid(1:3, id = "i3"))
41
tools::assertError(NN (1:3))# in all R versions
42
nn <- myN(2*pi)
43
##                     # current R  |  (not existing)
44
##                     # -----------|----------
45
class(getDataPart(m1)) # integer    |  numeric
46
class(getDataPart(m2)) # integer    |  numeric
47
## check for now [[conceivably, these *could* change !]] :
48
stopifnot(identical(getDataPart(m1), 1:3),
49
	  identical(getDataPart(m2), 1:3),
50
	  identical(S3Part(m1, strict=TRUE), 1:3),
51
	  identical(S3Part(m2, strict=TRUE), 1:3),
52
	  identical(2*pi, S3Part(nn, strict = TRUE)))
70613 maechler 53
 
54
if(FALSE) ## --- all these fail still:
55
stopifnot(
56
    identical(as(1L, "numeric"), as.numeric(1L))
57
    ,
58
    identical(as(1L, "numeric"), 1.0)
59
    ,
60
    is.double(as(1L, "double"))
61
    )