How can I unit test for a stack overflow in my factorial function?

user6800688

I made a factorial function in Racket, which needs to be unit tested for passing passing a very large number that would catch an overflow exception. If the exception is caught, the test should pass, and vice versa. Here is my code.

#lang racket

(provide recursive_factorial)
(provide tail_factorial)


(define (recursive_factorial number)
 (cond [(= 0 number) 1]
       [(negative? number) (raise-argument-error 'recursive_factorial "negative?" number)]
       [(*  number (recursive_factorial (- number 1)))]))


(define (tail_factorial number accumulator)

 (cond[( = number 0 ) accumulator]
      [(negative? number) accumulator (raise-argument-error 'tail_factorial  "negative?" number accumulator)]
      [(tail_factorial (- number 1) (* accumulator number ))]
      ))

And here is my attempt to unit test it.

(check-not-exn (λ () (recursive_factorial(100000000)))"stack overflow")
 (check-not-exn (λ () (tail_factorial(100000000)))"stack overflow")

With much help I was able to get a negative condition to work. Any help is appreciated.

soegaard

Your function returns -1 when number is negative. So shouldn't the test be:

(check-equal? (recursive_factorial -4) -1)

UPDATE

How about:

 #lang racket
 (provide recursive_factorial)
 (define (recursive_factorial number)
  (cond [(= 0 number)       1]
        [(negative? number) (error 'recursive_factorial
                                   "Cannot pass a negative number")]
        [else               (*  number (recursive_factorial (- number 1)))]))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make this F# function not cause a stack overflow

From Dev

How can I tell that my program is under unit test environment

From Dev

how can I write unit test to my meteor methods?

From Dev

how can I control the browser agent in my wicket unit test

From Dev

Jasmine Unit Testing: How can I spy on Function.prototype.call() in a function parameter that is passed to my method under test?

From Dev

How can I test for equality to a bound function when unit testing?

From Dev

Stack overflow with assoc and a function test

From Dev

stack overflow error while implementing a Recursive Function(factorial)

From Dev

stack overflow error while implementing a Recursive Function(factorial)

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

How can I make my factorial method work with decimals? (Gamma)

From Dev

How can I make my factorial method work with decimals? (Gamma)

From Dev

How can I unit test my project, when most of it access a database with a poor schema that I cannot change?

From Dev

How can I unit test Alex code?

From Dev

How can I unit test Alex code?

From Dev

How can I unit test that my Android Service launched a particular Activity?

From Dev

How can I reference unit test classes of a maven dependency in my java project?

From Dev

How do I unit test and integration test my SSIS packages?

From Dev

Not getting how to write unit test function of my function

From Dev

How can I safely unit test a function without testing every possible 64-bit input value?

From Dev

How do i Unit Test my RelayCommand using Moq?

From Dev

How can I encapsulate modules I want to unit test in Meteor?

From Dev

How to structure my unit test?

From Dev

How can I convert this large factorial function to a higher-order function?

From Dev

How to avoid stack overflow in this code, (Recursive function)

From Dev

how to stop void function from stack overflow

From Dev

How can I test my LWRP with ChefSpec?

From Dev

How can I test my classifier for overfitting?

Related Related

  1. 1

    How can I make this F# function not cause a stack overflow

  2. 2

    How can I tell that my program is under unit test environment

  3. 3

    how can I write unit test to my meteor methods?

  4. 4

    how can I control the browser agent in my wicket unit test

  5. 5

    Jasmine Unit Testing: How can I spy on Function.prototype.call() in a function parameter that is passed to my method under test?

  6. 6

    How can I test for equality to a bound function when unit testing?

  7. 7

    Stack overflow with assoc and a function test

  8. 8

    stack overflow error while implementing a Recursive Function(factorial)

  9. 9

    stack overflow error while implementing a Recursive Function(factorial)

  10. 10

    How do I mock controller context in my unit test so that my partial view to string function works?

  11. 11

    How do I mock controller context in my unit test so that my partial view to string function works?

  12. 12

    How can I make my factorial method work with decimals? (Gamma)

  13. 13

    How can I make my factorial method work with decimals? (Gamma)

  14. 14

    How can I unit test my project, when most of it access a database with a poor schema that I cannot change?

  15. 15

    How can I unit test Alex code?

  16. 16

    How can I unit test Alex code?

  17. 17

    How can I unit test that my Android Service launched a particular Activity?

  18. 18

    How can I reference unit test classes of a maven dependency in my java project?

  19. 19

    How do I unit test and integration test my SSIS packages?

  20. 20

    Not getting how to write unit test function of my function

  21. 21

    How can I safely unit test a function without testing every possible 64-bit input value?

  22. 22

    How do i Unit Test my RelayCommand using Moq?

  23. 23

    How can I encapsulate modules I want to unit test in Meteor?

  24. 24

    How to structure my unit test?

  25. 25

    How can I convert this large factorial function to a higher-order function?

  26. 26

    How to avoid stack overflow in this code, (Recursive function)

  27. 27

    how to stop void function from stack overflow

  28. 28

    How can I test my LWRP with ChefSpec?

  29. 29

    How can I test my classifier for overfitting?

HotTag

Archive