PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/inst/tests/test-is-type.R

https://bitbucket.org/fmark/assertive
R | 442 lines | 335 code | 107 blank | 0 comment | 0 complexity | e9ae6bc87c9682aa8b664fba6e583165 MD5 | raw file
  1. test.is2.1_to_5_is_numeric.returns_true <- function()
  2. {
  3. checkTrue(is2(1:5, "numeric"))
  4. }
  5. test.is2.1_to_5_is_list.returns_false <- function()
  6. {
  7. checkTrue(!is2(1:5, "list"))
  8. }
  9. test.is2.1_to_5_is_nonsense.returns_false <- function()
  10. {
  11. checkTrue(!is2(1:5, "a b c"))
  12. }
  13. test.is_array.an_array.returns_true <- function()
  14. {
  15. checkTrue(is_array(array()))
  16. }
  17. test.is_array.a_matrix.returns_true <- function()
  18. {
  19. checkTrue(is_array(matrix()))
  20. }
  21. test.is_array.a_data.frame.returns_false <- function()
  22. {
  23. checkTrue(!is_array(data.frame(x = 1:5)))
  24. }
  25. test.is_array.a_vector.returns_false <- function()
  26. {
  27. checkTrue(!is_array(1:10))
  28. }
  29. test.is_call.a_call.returns_true <- function()
  30. {
  31. checkTrue(is_call(call("sin", "pi")))
  32. }
  33. test.is_atomic.not_a_call.returns_false <- function()
  34. {
  35. checkTrue(!is_call(expression(sin(pi))))
  36. }
  37. test.is_character.character_vector.returns_true <- function()
  38. {
  39. checkTrue(is_character(letters))
  40. }
  41. test.is_character.NA_character_.returns_true <- function()
  42. {
  43. checkTrue(is_character(NA_character_))
  44. }
  45. test.is_character.not_a_character_vector.returns_false <- function()
  46. {
  47. checkTrue(!is_character(1:10))
  48. }
  49. test.is_class.lm_numeric_raster.returns_true <- function()
  50. {
  51. x <- c("lm", "numeric", "raster")
  52. expected <- c(TRUE, TRUE, FALSE)
  53. names(expected) <- x
  54. checkEquals(
  55. expected,
  56. is_class(x)
  57. )
  58. }
  59. test.is_complex.1i.returns_true <- function()
  60. {
  61. checkTrue(is_complex(1i))
  62. }
  63. test.is_complex.1.returns_false <- function()
  64. {
  65. checkTrue(!is_complex(1L))
  66. }
  67. test.is_complex.1_plus_0i.returns_true <- function()
  68. {
  69. checkTrue(is_complex(1 + 0i))
  70. }
  71. test.is_complex.na_complex_.returns_true <- function()
  72. {
  73. checkTrue(is_complex(NA_complex_))
  74. }
  75. test.is_data.frame.a_data.frame.returns_true <- function()
  76. {
  77. checkTrue(is_data.frame(data.frame(x = 1:5)))
  78. }
  79. test.is_data.frame.not_a_data.frame.returns_false <- function()
  80. {
  81. checkTrue(!is_data.frame(list(x = 1:5)))
  82. }
  83. test.is_environment.an_environment.returns_true <- function()
  84. {
  85. checkTrue(is_environment(new.env()))
  86. }
  87. test.is_environment.global_environment.returns_true <- function()
  88. {
  89. checkTrue(is_environment(globalenv()))
  90. }
  91. test.is_environment.base_environment.returns_true <- function()
  92. {
  93. checkTrue(is_environment(baseenv()))
  94. }
  95. test.is_environment.not_an_environment.returns_false <- function()
  96. {
  97. checkTrue(!is_environment(list()))
  98. }
  99. test.is_expression.an_environment.returns_true <- function()
  100. {
  101. checkTrue(is_expression(expression(sin(pi))))
  102. }
  103. test.is_expression.not_an_expression.returns_false <- function()
  104. {
  105. checkTrue(!is_expression(call("sin", "pi")))
  106. }
  107. test.is_factor.a_factor.returns_true <- function()
  108. {
  109. checkTrue(is_factor(factor(letters)))
  110. }
  111. test.is_factor.an_ordered_factor.returns_true <- function()
  112. {
  113. checkTrue(is_factor(ordered(letters, levels = letters)))
  114. }
  115. test.is_factor.not_a_factor.returns_false <- function()
  116. {
  117. checkTrue(!is_factor(letters))
  118. }
  119. test.is_function.a_function.returns_true <- function()
  120. {
  121. checkTrue(is_function(function(){}))
  122. }
  123. test.is_function.a_primitive_function.returns_true <- function()
  124. {
  125. checkTrue(is_function(sqrt))
  126. }
  127. test.is_function.not_a_function.returns_false <- function()
  128. {
  129. checkTrue(!is_function(call("sin", "pi")))
  130. }
  131. test.is_integer.an_integer_vector.returns_true <- function()
  132. {
  133. checkTrue(is_integer(1L:10L))
  134. }
  135. test.is_integer.na_integer_.returns_true <- function()
  136. {
  137. checkTrue(is_integer(NA_integer_))
  138. }
  139. test.is_integer.not_an_integer.returns_false <- function()
  140. {
  141. checkTrue(!is_integer(pi:10))
  142. }
  143. test.is_language.a_call.returns_true <- function()
  144. {
  145. checkTrue(is_language(call("sin", "pi")))
  146. }
  147. test.is_language.an_expression.returns_true <- function()
  148. {
  149. checkTrue(is_language(expression(sin(pi))))
  150. }
  151. test.is_language.a_name.returns_true <- function()
  152. {
  153. checkTrue(is_language(as.name("foo")))
  154. }
  155. test.is_language.not_a_language_object.returns_false <- function()
  156. {
  157. checkTrue(!is_language(sin))
  158. }
  159. test.is_leaf.a_leaf.returns_true <- function()
  160. {
  161. x <- structure(list(), leaf = TRUE)
  162. checkTrue(is_leaf(x))
  163. }
  164. test.is_leaf.a_null_leaf.returns_false <- function()
  165. {
  166. x <- list()
  167. checkTrue(!is_leaf(x))
  168. }
  169. test.is_leaf.a_non_logical_leaf.returns_false <- function()
  170. {
  171. x <- structure(list(), leaf = 1:10)
  172. checkTrue(!is_leaf(x))
  173. }
  174. test.is_leaf.a_false_leaf.returns_false <- function()
  175. {
  176. x <- structure(list(), leaf = FALSE)
  177. checkTrue(!is_leaf(x))
  178. }
  179. test.is_list.a_list.returns_true <- function()
  180. {
  181. checkTrue(is_list(list(1,2,3)))
  182. }
  183. test.is_list.an_atomic_vector.returns_false <- function()
  184. {
  185. checkTrue(!is_list(1:10))
  186. }
  187. test.is_list.null.returns_false <- function()
  188. {
  189. checkTrue(!is_list(NULL))
  190. }
  191. test.is_logical.a_logical_vector.returns_true <- function()
  192. {
  193. checkTrue(is_logical(c(TRUE, FALSE)))
  194. }
  195. test.is_logical.na.returns_true <- function()
  196. {
  197. checkTrue(is_logical(NA))
  198. }
  199. test.is_logical.not_a_logical.returns_false <- function()
  200. {
  201. checkTrue(!is_logical(1:10))
  202. }
  203. test.is_matrix.a_matrix.returns_true <- function()
  204. {
  205. checkTrue(is_matrix(matrix()))
  206. }
  207. test.is_matrix.an_array.returns_false <- function()
  208. {
  209. checkTrue(!is_matrix(array()))
  210. }
  211. test.is_matrix.a_data.frame.returns_false <- function()
  212. {
  213. checkTrue(!is_matrix(data.frame(x = 1:5)))
  214. }
  215. test.is_matrix.a_vector.returns_false <- function()
  216. {
  217. checkTrue(!is_matrix(1:10))
  218. }
  219. test.is_name.a_name.returns_true <- function()
  220. {
  221. checkTrue(is_name(as.name("foo")))
  222. }
  223. test.is_name.not_a_name.returns_false <- function()
  224. {
  225. checkTrue(!is_name(call("sin", "pi")))
  226. }
  227. test.is_numeric.a_numeric_vector.returns_true <- function()
  228. {
  229. checkTrue(is_numeric(1:10))
  230. }
  231. test.is_numeric.an_integer_vector.returns_true <- function()
  232. {
  233. checkTrue(is_numeric(1L:10L))
  234. }
  235. test.is_numeric.not_numeric.returns_false <- function()
  236. {
  237. checkTrue(!is_numeric(c(TRUE, FALSE)))
  238. }
  239. test.is_ordered.an_ordered_factor.returns_true <- function()
  240. {
  241. checkTrue(is_ordered(ordered(letters, levels = letters)))
  242. }
  243. test.is_ordered.an_unordered_factor.returns_false <- function()
  244. {
  245. checkTrue(!is_ordered(factor(letters)))
  246. }
  247. test.is_ordered.not_a_factor.returns_false <- function()
  248. {
  249. checkTrue(!is_ordered(letters))
  250. }
  251. test.is_primitive.a_primitive_function.returns_true <- function()
  252. {
  253. checkTrue(is_primitive(sqrt))
  254. }
  255. test.is_primitive.a_regular_function.returns_false <- function()
  256. {
  257. checkTrue(!is_primitive(function(){}))
  258. }
  259. test.is_primitive.not_a_function.returns_false <- function()
  260. {
  261. checkTrue(!is_primitive(call("sin", "pi")))
  262. }
  263. test.is_raster.a_raster.returns_true <- function()
  264. {
  265. m <- matrix(hcl(0, 80, seq(50, 80, 10)), nrow=4, ncol=5)
  266. checkTrue(is_raster(as.raster(m)))
  267. }
  268. test.is_raster.a_matrix.returns_false <- function()
  269. {
  270. m <- matrix(hcl(0, 80, seq(50, 80, 10)), nrow=4, ncol=5)
  271. checkTrue(!is_raster(m))
  272. }
  273. test.is_raw.a_raw_vector.returns_true <- function()
  274. {
  275. checkTrue(is_raw(as.raw(1:10)))
  276. }
  277. test.is_raw.not_raw.returns_false <- function()
  278. {
  279. checkTrue(!is_raw(c(TRUE, FALSE)))
  280. }
  281. test.is_relistable.a_relistable_object.returns_true <- function()
  282. {
  283. checkTrue(is_relistable(as.relistable(list(1,2,3))))
  284. }
  285. test.is_relistable.not_relistable.returns_false <- function()
  286. {
  287. checkTrue(!is_relistable(list(1,2,3)))
  288. }
  289. test.is_S4.an_S4_instance.returns_true <- function()
  290. {
  291. x <- getClass("MethodDefinition")
  292. checkTrue(is_S4(x))
  293. }
  294. test.is_S4.not_an_S4_instance.returns_true <- function()
  295. {
  296. checkTrue(!is_S4(1:10))
  297. }
  298. test.is_stepfun.a_step_function.returns_true <- function()
  299. {
  300. x <- stepfun(1:3, c(1, 2, 4, 3), f = 0)
  301. checkTrue(is_stepfun(x))
  302. }
  303. test.is_stepfun.a_regular_function.returns_false <- function()
  304. {
  305. checkTrue(!is_stepfun(function(){}))
  306. }
  307. test.is_stepfun.not_a_function.returns_false <- function()
  308. {
  309. checkTrue(!is_stepfun(call("sin", "pi")))
  310. }
  311. test.is_table.a_table.returns_true <- function()
  312. {
  313. x <- table(sample(letters, 100, replace = TRUE))
  314. checkTrue(is_table(x))
  315. }
  316. test.is_table.not_a_table.returns_false <- function()
  317. {
  318. checkTrue(!is_table(1:10))
  319. }
  320. test.is_ts.a_time_series.returns_true <- function()
  321. {
  322. checkTrue(is_ts(ts(1:10)))
  323. }
  324. test.is_ts.not_a_time_series.returns_false <- function()
  325. {
  326. checkTrue(!is_ts(1:10))
  327. }
  328. test.is_tskernel.a_time_series_kernel.returns_true <- function()
  329. {
  330. checkTrue(is_tskernel(kernel("daniell", 10)))
  331. }
  332. test.is_tskernel.not_a_time_series_kernel.returns_false <- function()
  333. {
  334. checkTrue(!is_tskernel(1:10))
  335. }