number.go 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. package httpexpect
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. )
  8. // Number provides methods to inspect attached float64 value
  9. // (Go representation of JSON number).
  10. type Number struct {
  11. noCopy noCopy
  12. chain *chain
  13. value float64
  14. }
  15. // NewNumber returns a new Number instance.
  16. //
  17. // If reporter is nil, the function panics.
  18. //
  19. // Example:
  20. //
  21. // number := NewNumber(t, 123.4)
  22. func NewNumber(reporter Reporter, value float64) *Number {
  23. return newNumber(newChainWithDefaults("Number()", reporter), value)
  24. }
  25. // NewNumberC returns a new Number instance with config.
  26. //
  27. // Requirements for config are same as for WithConfig function.
  28. //
  29. // Example:
  30. //
  31. // number := NewNumberC(config, 123.4)
  32. func NewNumberC(config Config, value float64) *Number {
  33. return newNumber(newChainWithConfig("Number()", config.withDefaults()), value)
  34. }
  35. func newNumber(parent *chain, val float64) *Number {
  36. return &Number{chain: parent.clone(), value: val}
  37. }
  38. // Raw returns underlying value attached to Number.
  39. // This is the value originally passed to NewNumber.
  40. //
  41. // Example:
  42. //
  43. // number := NewNumber(t, 123.4)
  44. // assert.Equal(t, 123.4, number.Raw())
  45. func (n *Number) Raw() float64 {
  46. return n.value
  47. }
  48. // Decode unmarshals the underlying value attached to the Number to a target variable.
  49. // target should be one of these:
  50. //
  51. // - pointer to an empty interface
  52. // - pointer to any integer or floating type
  53. //
  54. // Example:
  55. //
  56. // value := NewNumber(t, 123)
  57. //
  58. // var target interface{}
  59. // valude.decode(&target)
  60. //
  61. // assert.Equal(t, 123, target)
  62. func (n *Number) Decode(target interface{}) *Number {
  63. opChain := n.chain.enter("Decode()")
  64. defer opChain.leave()
  65. if opChain.failed() {
  66. return n
  67. }
  68. canonDecode(opChain, n.value, target)
  69. return n
  70. }
  71. // Alias is similar to Value.Alias.
  72. func (n *Number) Alias(name string) *Number {
  73. opChain := n.chain.enter("Alias(%q)", name)
  74. defer opChain.leave()
  75. n.chain.setAlias(name)
  76. return n
  77. }
  78. // Path is similar to Value.Path.
  79. func (n *Number) Path(path string) *Value {
  80. opChain := n.chain.enter("Path(%q)", path)
  81. defer opChain.leave()
  82. return jsonPath(opChain, n.value, path)
  83. }
  84. // Schema is similar to Value.Schema.
  85. func (n *Number) Schema(schema interface{}) *Number {
  86. opChain := n.chain.enter("Schema()")
  87. defer opChain.leave()
  88. jsonSchema(opChain, n.value, schema)
  89. return n
  90. }
  91. // IsEqual succeeds if number is equal to given value.
  92. //
  93. // value should have numeric type convertible to float64. Before comparison,
  94. // it is converted to float64.
  95. //
  96. // Example:
  97. //
  98. // number := NewNumber(t, 123)
  99. // number.IsEqual(float64(123))
  100. // number.IsEqual(int32(123))
  101. func (n *Number) IsEqual(value interface{}) *Number {
  102. opChain := n.chain.enter("IsEqual()")
  103. defer opChain.leave()
  104. if opChain.failed() {
  105. return n
  106. }
  107. num, ok := canonNumber(opChain, value)
  108. if !ok {
  109. return n
  110. }
  111. if !(n.value == num) {
  112. opChain.fail(AssertionFailure{
  113. Type: AssertEqual,
  114. Actual: &AssertionValue{n.value},
  115. Expected: &AssertionValue{num},
  116. Errors: []error{
  117. errors.New("expected: numbers are equal"),
  118. },
  119. })
  120. }
  121. return n
  122. }
  123. // NotEqual succeeds if number is not equal to given value.
  124. //
  125. // value should have numeric type convertible to float64. Before comparison,
  126. // it is converted to float64.
  127. //
  128. // Example:
  129. //
  130. // number := NewNumber(t, 123)
  131. // number.NotEqual(float64(321))
  132. // number.NotEqual(int32(321))
  133. func (n *Number) NotEqual(value interface{}) *Number {
  134. opChain := n.chain.enter("NotEqual()")
  135. defer opChain.leave()
  136. if opChain.failed() {
  137. return n
  138. }
  139. num, ok := canonNumber(opChain, value)
  140. if !ok {
  141. return n
  142. }
  143. if n.value == num {
  144. opChain.fail(AssertionFailure{
  145. Type: AssertNotEqual,
  146. Actual: &AssertionValue{n.value},
  147. Expected: &AssertionValue{num},
  148. Errors: []error{
  149. errors.New("expected: numbers are non-equal"),
  150. },
  151. })
  152. }
  153. return n
  154. }
  155. // Deprecated: use IsEqual instead.
  156. func (n *Number) Equal(value interface{}) *Number {
  157. return n.IsEqual(value)
  158. }
  159. // InDelta succeeds if two numerals are within delta of each other.
  160. //
  161. // Example:
  162. //
  163. // number := NewNumber(t, 123.0)
  164. // number.InDelta(123.2, 0.3)
  165. func (n *Number) InDelta(value, delta float64) *Number {
  166. opChain := n.chain.enter("InDelta()")
  167. defer opChain.leave()
  168. if opChain.failed() {
  169. return n
  170. }
  171. if math.IsNaN(n.value) || math.IsNaN(value) || math.IsNaN(delta) {
  172. opChain.fail(AssertionFailure{
  173. Type: AssertEqual,
  174. Actual: &AssertionValue{n.value},
  175. Expected: &AssertionValue{value},
  176. Delta: &AssertionValue{delta},
  177. Errors: []error{
  178. errors.New("expected: numbers are comparable"),
  179. },
  180. })
  181. return n
  182. }
  183. diff := n.value - value
  184. if diff < -delta || diff > delta {
  185. opChain.fail(AssertionFailure{
  186. Type: AssertEqual,
  187. Actual: &AssertionValue{n.value},
  188. Expected: &AssertionValue{value},
  189. Delta: &AssertionValue{delta},
  190. Errors: []error{
  191. errors.New("expected: numbers lie within delta"),
  192. },
  193. })
  194. return n
  195. }
  196. return n
  197. }
  198. // NotInDelta succeeds if two numerals are not within delta of each other.
  199. //
  200. // Example:
  201. //
  202. // number := NewNumber(t, 123.0)
  203. // number.NotInDelta(123.2, 0.1)
  204. func (n *Number) NotInDelta(value, delta float64) *Number {
  205. opChain := n.chain.enter("NotInDelta()")
  206. defer opChain.leave()
  207. if opChain.failed() {
  208. return n
  209. }
  210. if math.IsNaN(n.value) || math.IsNaN(value) || math.IsNaN(delta) {
  211. opChain.fail(AssertionFailure{
  212. Type: AssertNotEqual,
  213. Actual: &AssertionValue{n.value},
  214. Expected: &AssertionValue{value},
  215. Delta: &AssertionValue{delta},
  216. Errors: []error{
  217. errors.New("expected: numbers are comparable"),
  218. },
  219. })
  220. return n
  221. }
  222. diff := n.value - value
  223. if !(diff < -delta || diff > delta) {
  224. opChain.fail(AssertionFailure{
  225. Type: AssertNotEqual,
  226. Actual: &AssertionValue{n.value},
  227. Expected: &AssertionValue{value},
  228. Delta: &AssertionValue{delta},
  229. Errors: []error{
  230. errors.New("expected: numbers do not lie within delta"),
  231. },
  232. })
  233. return n
  234. }
  235. return n
  236. }
  237. // Deprecated: use InDelta instead.
  238. func (n *Number) EqualDelta(value, delta float64) *Number {
  239. return n.InDelta(value, delta)
  240. }
  241. // Deprecated: use NotInDelta instead.
  242. func (n *Number) NotEqualDelta(value, delta float64) *Number {
  243. return n.NotInDelta(value, delta)
  244. }
  245. // InRange succeeds if number is within given range [min; max].
  246. //
  247. // min and max should have numeric type convertible to float64. Before comparison,
  248. // they are converted to float64.
  249. //
  250. // Example:
  251. //
  252. // number := NewNumber(t, 123)
  253. // number.InRange(float32(100), int32(200)) // success
  254. // number.InRange(100, 200) // success
  255. // number.InRange(123, 123) // success
  256. func (n *Number) InRange(min, max interface{}) *Number {
  257. opChain := n.chain.enter("InRange()")
  258. defer opChain.leave()
  259. if opChain.failed() {
  260. return n
  261. }
  262. a, ok := canonNumber(opChain, min)
  263. if !ok {
  264. return n
  265. }
  266. b, ok := canonNumber(opChain, max)
  267. if !ok {
  268. return n
  269. }
  270. if !(n.value >= a && n.value <= b) {
  271. opChain.fail(AssertionFailure{
  272. Type: AssertInRange,
  273. Actual: &AssertionValue{n.value},
  274. Expected: &AssertionValue{AssertionRange{a, b}},
  275. Errors: []error{
  276. errors.New("expected: number is within given range"),
  277. },
  278. })
  279. }
  280. return n
  281. }
  282. // NotInRange succeeds if number is not within given range [min; max].
  283. //
  284. // min and max should have numeric type convertible to float64. Before comparison,
  285. // they are converted to float64.
  286. //
  287. // Example:
  288. //
  289. // number := NewNumber(t, 100)
  290. // number.NotInRange(0, 99)
  291. // number.NotInRange(101, 200)
  292. func (n *Number) NotInRange(min, max interface{}) *Number {
  293. opChain := n.chain.enter("NotInRange()")
  294. defer opChain.leave()
  295. if opChain.failed() {
  296. return n
  297. }
  298. a, ok := canonNumber(opChain, min)
  299. if !ok {
  300. return n
  301. }
  302. b, ok := canonNumber(opChain, max)
  303. if !ok {
  304. return n
  305. }
  306. if n.value >= a && n.value <= b {
  307. opChain.fail(AssertionFailure{
  308. Type: AssertNotInRange,
  309. Actual: &AssertionValue{n.value},
  310. Expected: &AssertionValue{AssertionRange{a, b}},
  311. Errors: []error{
  312. errors.New("expected: number is not within given range"),
  313. },
  314. })
  315. }
  316. return n
  317. }
  318. // InList succeeds if the number is equal to one of the values from given list
  319. // of numbers. Before comparison, each value is converted to canonical form.
  320. //
  321. // Each value should be numeric type convertible to float64. If at least one
  322. // value has wrong type, failure is reported.
  323. //
  324. // Example:
  325. //
  326. // number := NewNumber(t, 123)
  327. // number.InList(float64(123), int32(123))
  328. func (n *Number) InList(values ...interface{}) *Number {
  329. opChain := n.chain.enter("IsList()")
  330. defer opChain.leave()
  331. if opChain.failed() {
  332. return n
  333. }
  334. if len(values) == 0 {
  335. opChain.fail(AssertionFailure{
  336. Type: AssertUsage,
  337. Errors: []error{
  338. errors.New("unexpected empty list argument"),
  339. },
  340. })
  341. return n
  342. }
  343. var isListed bool
  344. for _, v := range values {
  345. num, ok := canonNumber(opChain, v)
  346. if !ok {
  347. return n
  348. }
  349. if n.value == num {
  350. isListed = true
  351. // continue loop to check that all values are correct
  352. }
  353. }
  354. if !isListed {
  355. opChain.fail(AssertionFailure{
  356. Type: AssertBelongs,
  357. Actual: &AssertionValue{n.value},
  358. Expected: &AssertionValue{AssertionList(values)},
  359. Errors: []error{
  360. errors.New("expected: number is equal to one of the values"),
  361. },
  362. })
  363. }
  364. return n
  365. }
  366. // NotInList succeeds if the number is not equal to any of the values from given
  367. // list of numbers. Before comparison, each value is converted to canonical form.
  368. //
  369. // Each value should be numeric type convertible to float64. If at least one
  370. // value has wrong type, failure is reported.
  371. //
  372. // Example:
  373. //
  374. // number := NewNumber(t, 123)
  375. // number.NotInList(float64(456), int32(456))
  376. func (n *Number) NotInList(values ...interface{}) *Number {
  377. opChain := n.chain.enter("NotInList()")
  378. defer opChain.leave()
  379. if opChain.failed() {
  380. return n
  381. }
  382. if len(values) == 0 {
  383. opChain.fail(AssertionFailure{
  384. Type: AssertUsage,
  385. Errors: []error{
  386. errors.New("unexpected empty list argument"),
  387. },
  388. })
  389. return n
  390. }
  391. for _, v := range values {
  392. num, ok := canonNumber(opChain, v)
  393. if !ok {
  394. return n
  395. }
  396. if n.value == num {
  397. opChain.fail(AssertionFailure{
  398. Type: AssertNotBelongs,
  399. Actual: &AssertionValue{n.value},
  400. Expected: &AssertionValue{AssertionList(values)},
  401. Errors: []error{
  402. errors.New("expected: number is not equal to any of the values"),
  403. },
  404. })
  405. return n
  406. }
  407. }
  408. return n
  409. }
  410. // Gt succeeds if number is greater than given value.
  411. //
  412. // value should have numeric type convertible to float64. Before comparison,
  413. // it is converted to float64.
  414. //
  415. // Example:
  416. //
  417. // number := NewNumber(t, 123)
  418. // number.Gt(float64(122))
  419. // number.Gt(int32(122))
  420. func (n *Number) Gt(value interface{}) *Number {
  421. opChain := n.chain.enter("Gt()")
  422. defer opChain.leave()
  423. if opChain.failed() {
  424. return n
  425. }
  426. num, ok := canonNumber(opChain, value)
  427. if !ok {
  428. return n
  429. }
  430. if !(n.value > num) {
  431. opChain.fail(AssertionFailure{
  432. Type: AssertGt,
  433. Actual: &AssertionValue{n.value},
  434. Expected: &AssertionValue{num},
  435. Errors: []error{
  436. errors.New("expected: number is larger than given value"),
  437. },
  438. })
  439. }
  440. return n
  441. }
  442. // Ge succeeds if number is greater than or equal to given value.
  443. //
  444. // value should have numeric type convertible to float64. Before comparison,
  445. // it is converted to float64.
  446. //
  447. // Example:
  448. //
  449. // number := NewNumber(t, 123)
  450. // number.Ge(float64(122))
  451. // number.Ge(int32(122))
  452. func (n *Number) Ge(value interface{}) *Number {
  453. opChain := n.chain.enter("Ge()")
  454. defer opChain.leave()
  455. if opChain.failed() {
  456. return n
  457. }
  458. num, ok := canonNumber(opChain, value)
  459. if !ok {
  460. return n
  461. }
  462. if !(n.value >= num) {
  463. opChain.fail(AssertionFailure{
  464. Type: AssertGe,
  465. Actual: &AssertionValue{n.value},
  466. Expected: &AssertionValue{num},
  467. Errors: []error{
  468. errors.New("expected: number is larger than or equal to given value"),
  469. },
  470. })
  471. }
  472. return n
  473. }
  474. // Lt succeeds if number is lesser than given value.
  475. //
  476. // value should have numeric type convertible to float64. Before comparison,
  477. // it is converted to float64.
  478. //
  479. // Example:
  480. //
  481. // number := NewNumber(t, 123)
  482. // number.Lt(float64(124))
  483. // number.Lt(int32(124))
  484. func (n *Number) Lt(value interface{}) *Number {
  485. opChain := n.chain.enter("Lt()")
  486. defer opChain.leave()
  487. if opChain.failed() {
  488. return n
  489. }
  490. num, ok := canonNumber(opChain, value)
  491. if !ok {
  492. return n
  493. }
  494. if !(n.value < num) {
  495. opChain.fail(AssertionFailure{
  496. Type: AssertLt,
  497. Actual: &AssertionValue{n.value},
  498. Expected: &AssertionValue{num},
  499. Errors: []error{
  500. errors.New("expected: number is less than given value"),
  501. },
  502. })
  503. }
  504. return n
  505. }
  506. // Le succeeds if number is lesser than or equal to given value.
  507. //
  508. // value should have numeric type convertible to float64. Before comparison,
  509. // it is converted to float64.
  510. //
  511. // Example:
  512. //
  513. // number := NewNumber(t, 123)
  514. // number.Le(float64(124))
  515. // number.Le(int32(124))
  516. func (n *Number) Le(value interface{}) *Number {
  517. opChain := n.chain.enter("Le()")
  518. defer opChain.leave()
  519. if opChain.failed() {
  520. return n
  521. }
  522. num, ok := canonNumber(opChain, value)
  523. if !ok {
  524. return n
  525. }
  526. if !(n.value <= num) {
  527. opChain.fail(AssertionFailure{
  528. Type: AssertLe,
  529. Actual: &AssertionValue{n.value},
  530. Expected: &AssertionValue{num},
  531. Errors: []error{
  532. errors.New("expected: number is less than or equal to given value"),
  533. },
  534. })
  535. }
  536. return n
  537. }
  538. // IsInt succeeds if number is a signed integer of the specified bit width
  539. // as an optional argument.
  540. //
  541. // Bits argument defines maximum allowed bitness for the given number.
  542. // If bits is omitted, boundary check is omitted too.
  543. //
  544. // Example:
  545. //
  546. // number := NewNumber(t, 1000000)
  547. // number.IsInt() // success
  548. // number.IsInt(32) // success
  549. // number.IsInt(16) // failure
  550. //
  551. // number := NewNumber(t, -1000000)
  552. // number.IsInt() // success
  553. // number.IsInt(32) // success
  554. // number.IsInt(16) // failure
  555. //
  556. // number := NewNumber(t, 0.5)
  557. // number.IsInt() // failure
  558. func (n *Number) IsInt(bits ...int) *Number {
  559. opChain := n.chain.enter("IsInt()")
  560. defer opChain.leave()
  561. if opChain.failed() {
  562. return n
  563. }
  564. if len(bits) > 1 {
  565. opChain.fail(AssertionFailure{
  566. Type: AssertUsage,
  567. Errors: []error{
  568. errors.New("unexpected multiple bits arguments"),
  569. },
  570. })
  571. return n
  572. }
  573. if len(bits) == 1 && bits[0] <= 0 {
  574. opChain.fail(AssertionFailure{
  575. Type: AssertUsage,
  576. Errors: []error{
  577. errors.New("unexpected non-positive bits argument"),
  578. },
  579. })
  580. return n
  581. }
  582. if math.IsNaN(n.value) {
  583. opChain.fail(AssertionFailure{
  584. Type: AssertValid,
  585. Actual: &AssertionValue{n.value},
  586. Errors: []error{
  587. errors.New("expected: number is signed integer"),
  588. },
  589. })
  590. return n
  591. }
  592. inum, acc := big.NewFloat(n.value).Int(nil)
  593. if !(acc == big.Exact) {
  594. opChain.fail(AssertionFailure{
  595. Type: AssertValid,
  596. Actual: &AssertionValue{n.value},
  597. Errors: []error{
  598. errors.New("expected: number is signed integer"),
  599. },
  600. })
  601. return n
  602. }
  603. if len(bits) > 0 {
  604. bitSize := bits[0]
  605. imax := new(big.Int)
  606. imax.Lsh(big.NewInt(1), uint(bitSize-1))
  607. imax.Sub(imax, big.NewInt(1))
  608. imin := new(big.Int)
  609. imin.Neg(imax)
  610. imin.Sub(imin, big.NewInt(1))
  611. if inum.Cmp(imin) < 0 || inum.Cmp(imax) > 0 {
  612. opChain.fail(AssertionFailure{
  613. Type: AssertInRange,
  614. Actual: &AssertionValue{n.value},
  615. Expected: &AssertionValue{AssertionRange{
  616. Min: intBoundary{imin, -1, bitSize - 1},
  617. Max: intBoundary{imax, +1, bitSize - 1},
  618. }},
  619. Errors: []error{
  620. fmt.Errorf("expected: number is %d-bit signed integer", bitSize),
  621. },
  622. })
  623. return n
  624. }
  625. }
  626. return n
  627. }
  628. // NotInt succeeds if number is not a signed integer of the specified bit
  629. // width as an optional argument.
  630. //
  631. // Bits argument defines maximum allowed bitness for the given number.
  632. // If bits is omitted, boundary check is omitted too.
  633. //
  634. // Example:
  635. //
  636. // number := NewNumber(t, 1000000)
  637. // number.NotInt() // failure
  638. // number.NotInt(32) // failure
  639. // number.NotInt(16) // success
  640. //
  641. // number := NewNumber(t, -1000000)
  642. // number.NotInt() // failure
  643. // number.NotInt(32) // failure
  644. // number.NotInt(16) // success
  645. //
  646. // number := NewNumber(t, 0.5)
  647. // number.NotInt() // success
  648. func (n *Number) NotInt(bits ...int) *Number {
  649. opChain := n.chain.enter("NotInt()")
  650. defer opChain.leave()
  651. if opChain.failed() {
  652. return n
  653. }
  654. if len(bits) > 1 {
  655. opChain.fail(AssertionFailure{
  656. Type: AssertUsage,
  657. Errors: []error{
  658. errors.New("unexpected multiple bits arguments"),
  659. },
  660. })
  661. return n
  662. }
  663. if len(bits) == 1 && bits[0] <= 0 {
  664. opChain.fail(AssertionFailure{
  665. Type: AssertUsage,
  666. Errors: []error{
  667. errors.New("unexpected non-positive bits argument"),
  668. },
  669. })
  670. return n
  671. }
  672. if !math.IsNaN(n.value) {
  673. inum, acc := big.NewFloat(n.value).Int(nil)
  674. if acc == big.Exact {
  675. if len(bits) == 0 {
  676. opChain.fail(AssertionFailure{
  677. Type: AssertValid,
  678. Actual: &AssertionValue{n.value},
  679. Errors: []error{
  680. errors.New("expected: number is not signed integer"),
  681. },
  682. })
  683. return n
  684. }
  685. bitSize := bits[0]
  686. imax := new(big.Int)
  687. imax.Lsh(big.NewInt(1), uint(bitSize-1))
  688. imax.Sub(imax, big.NewInt(1))
  689. imin := new(big.Int)
  690. imin.Neg(imax)
  691. imin.Sub(imin, big.NewInt(1))
  692. if !(inum.Cmp(imin) < 0 || inum.Cmp(imax) > 0) {
  693. opChain.fail(AssertionFailure{
  694. Type: AssertNotInRange,
  695. Actual: &AssertionValue{n.value},
  696. Expected: &AssertionValue{AssertionRange{
  697. Min: intBoundary{imin, -1, bitSize - 1},
  698. Max: intBoundary{imax, +1, bitSize - 1},
  699. }},
  700. Errors: []error{
  701. fmt.Errorf(
  702. "expected: number doesn't fit %d-bit signed integer",
  703. bitSize),
  704. },
  705. })
  706. return n
  707. }
  708. }
  709. }
  710. return n
  711. }
  712. // IsUint succeeds if number is an unsigned integer of the specified bit
  713. // width as an optional argument.
  714. //
  715. // Bits argument defines maximum allowed bitness for the given number.
  716. // If bits is omitted, boundary check is omitted too.
  717. //
  718. // Example:
  719. //
  720. // number := NewNumber(t, 1000000)
  721. // number.IsUint() // success
  722. // number.IsUint(32) // success
  723. // number.IsUint(16) // failure
  724. //
  725. // number := NewNumber(t, -1000000)
  726. // number.IsUint() // failure
  727. // number.IsUint(32) // failure
  728. // number.IsUint(16) // failure
  729. //
  730. // number := NewNumber(t, 0.5)
  731. // number.IsUint() // failure
  732. func (n *Number) IsUint(bits ...int) *Number {
  733. opChain := n.chain.enter("IsUint()")
  734. defer opChain.leave()
  735. if opChain.failed() {
  736. return n
  737. }
  738. if len(bits) > 1 {
  739. opChain.fail(AssertionFailure{
  740. Type: AssertUsage,
  741. Errors: []error{
  742. errors.New("unexpected multiple bits arguments"),
  743. },
  744. })
  745. return n
  746. }
  747. if len(bits) == 1 && bits[0] <= 0 {
  748. opChain.fail(AssertionFailure{
  749. Type: AssertUsage,
  750. Errors: []error{
  751. errors.New("unexpected non-positive bits argument"),
  752. },
  753. })
  754. return n
  755. }
  756. if math.IsNaN(n.value) {
  757. opChain.fail(AssertionFailure{
  758. Type: AssertValid,
  759. Actual: &AssertionValue{n.value},
  760. Errors: []error{
  761. errors.New("expected: number is unsigned integer"),
  762. },
  763. })
  764. return n
  765. }
  766. inum, acc := big.NewFloat(n.value).Int(nil)
  767. if !(acc == big.Exact) {
  768. opChain.fail(AssertionFailure{
  769. Type: AssertValid,
  770. Actual: &AssertionValue{n.value},
  771. Errors: []error{
  772. errors.New("expected: number is unsigned integer"),
  773. },
  774. })
  775. return n
  776. }
  777. imin := big.NewInt(0)
  778. if inum.Cmp(imin) < 0 {
  779. opChain.fail(AssertionFailure{
  780. Type: AssertValid,
  781. Actual: &AssertionValue{n.value},
  782. Errors: []error{
  783. errors.New("expected: number is unsigned integer"),
  784. },
  785. })
  786. return n
  787. }
  788. if len(bits) > 0 {
  789. bitSize := bits[0]
  790. imax := new(big.Int)
  791. imax.Lsh(big.NewInt(1), uint(bitSize))
  792. imax.Sub(imax, big.NewInt(1))
  793. if inum.Cmp(imax) > 0 {
  794. opChain.fail(AssertionFailure{
  795. Type: AssertInRange,
  796. Actual: &AssertionValue{n.value},
  797. Expected: &AssertionValue{AssertionRange{
  798. Min: intBoundary{imin, 0, 0},
  799. Max: intBoundary{imax, +1, bitSize},
  800. }},
  801. Errors: []error{
  802. fmt.Errorf("expected: number fits %d-bit unsigned integer", bitSize),
  803. },
  804. })
  805. return n
  806. }
  807. }
  808. return n
  809. }
  810. // NotUint succeeds if number is not an unsigned integer of the specified bit
  811. // width as an optional argument.
  812. //
  813. // Bits argument defines maximum allowed bitness for the given number.
  814. // If bits is omitted, boundary check is omitted too.
  815. //
  816. // Example:
  817. //
  818. // number := NewNumber(t, 1000000)
  819. // number.NotUint() // failure
  820. // number.NotUint(32) // failure
  821. // number.NotUint(16) // success
  822. //
  823. // number := NewNumber(t, -1000000)
  824. // number.NotUint() // success
  825. // number.NotUint(32) // success
  826. // number.NotUint(16) // success
  827. //
  828. // number := NewNumber(t, 0.5)
  829. // number.NotUint() // success
  830. func (n *Number) NotUint(bits ...int) *Number {
  831. opChain := n.chain.enter("NotUint()")
  832. defer opChain.leave()
  833. if opChain.failed() {
  834. return n
  835. }
  836. if len(bits) > 1 {
  837. opChain.fail(AssertionFailure{
  838. Type: AssertUsage,
  839. Errors: []error{
  840. errors.New("unexpected multiple bits arguments"),
  841. },
  842. })
  843. return n
  844. }
  845. if len(bits) == 1 && bits[0] <= 0 {
  846. opChain.fail(AssertionFailure{
  847. Type: AssertUsage,
  848. Errors: []error{
  849. errors.New("unexpected non-positive bits argument"),
  850. },
  851. })
  852. return n
  853. }
  854. if !math.IsNaN(n.value) {
  855. inum, acc := big.NewFloat(n.value).Int(nil)
  856. if acc == big.Exact {
  857. imin := big.NewInt(0)
  858. if inum.Cmp(imin) >= 0 {
  859. if len(bits) == 0 {
  860. opChain.fail(AssertionFailure{
  861. Type: AssertValid,
  862. Actual: &AssertionValue{n.value},
  863. Errors: []error{
  864. errors.New("expected: number is not unsigned integer"),
  865. },
  866. })
  867. return n
  868. }
  869. bitSize := bits[0]
  870. imax := new(big.Int)
  871. imax.Lsh(big.NewInt(1), uint(bitSize))
  872. imax.Sub(imax, big.NewInt(1))
  873. if inum.Cmp(imax) <= 0 {
  874. opChain.fail(AssertionFailure{
  875. Type: AssertNotInRange,
  876. Actual: &AssertionValue{n.value},
  877. Expected: &AssertionValue{AssertionRange{
  878. Min: intBoundary{imin, 0, 0},
  879. Max: intBoundary{imax, +1, bitSize},
  880. }},
  881. Errors: []error{
  882. fmt.Errorf(
  883. "expected: number doesn't fit %d-bit unsigned integer",
  884. bitSize),
  885. },
  886. })
  887. return n
  888. }
  889. }
  890. }
  891. }
  892. return n
  893. }
  894. // IsFinite succeeds if number is neither ±Inf nor NaN.
  895. //
  896. // Example:
  897. //
  898. // number := NewNumber(t, 1234.5)
  899. // number.IsFinite() // success
  900. //
  901. // number := NewNumber(t, math.NaN())
  902. // number.IsFinite() // failure
  903. //
  904. // number := NewNumber(t, math.Inf(+1))
  905. // number.IsFinite() // failure
  906. func (n *Number) IsFinite() *Number {
  907. opChain := n.chain.enter("IsFinite()")
  908. defer opChain.leave()
  909. if opChain.failed() {
  910. return n
  911. }
  912. if math.IsInf(n.value, 0) || math.IsNaN(n.value) {
  913. opChain.fail(AssertionFailure{
  914. Type: AssertValid,
  915. Actual: &AssertionValue{n.value},
  916. Errors: []error{
  917. errors.New("expected: number is neither ±Inf nor NaN"),
  918. },
  919. })
  920. return n
  921. }
  922. return n
  923. }
  924. // NotFinite succeeds if number is either ±Inf or NaN.
  925. //
  926. // Example:
  927. //
  928. // number := NewNumber(t, 1234.5)
  929. // number.NotFinite() // failure
  930. //
  931. // number := NewNumber(t, math.NaN())
  932. // number.NotFinite() // success
  933. //
  934. // number := NewNumber(t, math.Inf(+1))
  935. // number.NotFinite() // success
  936. func (n *Number) NotFinite() *Number {
  937. opChain := n.chain.enter("NotFinite()")
  938. defer opChain.leave()
  939. if opChain.failed() {
  940. return n
  941. }
  942. if !(math.IsInf(n.value, 0) || math.IsNaN(n.value)) {
  943. opChain.fail(AssertionFailure{
  944. Type: AssertValid,
  945. Actual: &AssertionValue{n.value},
  946. Errors: []error{
  947. errors.New("expected: number is either ±Inf or NaN"),
  948. },
  949. })
  950. return n
  951. }
  952. return n
  953. }
  954. type intBoundary struct {
  955. val *big.Int
  956. sign int
  957. bits int
  958. }
  959. func (b intBoundary) String() string {
  960. if b.sign > 0 {
  961. return fmt.Sprintf("+2^%d-1 (+%s)", b.bits, b.val)
  962. } else if b.sign < 0 {
  963. return fmt.Sprintf("-2^%d (%s)", b.bits, b.val)
  964. }
  965. return fmt.Sprintf("%s", b.val)
  966. }