~rhcarvalho/+junk/racket

« back to all changes in this revision

Viewing changes to demo/fizzbuzz.rkt

  • Committer: Rodolfo Carvalho
  • Date: 2011-05-18 02:33:38 UTC
  • Revision ID: rhcarvalho@gmail.com-20110518023338-w9aon2m9v4fq2f80
Add some demo code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#lang racket/base
 
2
(require rackunit rackunit/gui)
 
3
 
 
4
(define (divisible? m n)
 
5
  (if (zero? n)
 
6
      #f
 
7
      (zero? (remainder m n))))
 
8
 
 
9
(define (fizzbuzz n)
 
10
  (cond
 
11
    [(divisible? n 15) "fizzbuzz"]
 
12
    [(divisible? n 3) "fizz"]
 
13
    [(divisible? n 5) "buzz"]
 
14
    [else n]))
 
15
 
 
16
(define-test-suite
 
17
  FizzBuzz
 
18
  (test-equal?
 
19
   "Regular numbers are not affected"
 
20
   (fizzbuzz 4) 4)
 
21
  
 
22
  (test-case
 
23
   "3 is fizz"
 
24
   (check-equal? (fizzbuzz 3) "fizz"))
 
25
  
 
26
  (test-case
 
27
   "5 is buzz"
 
28
   (check-equal? (fizzbuzz 5) "buzz"))
 
29
  
 
30
  (test-equal?
 
31
   "15 is fizzbuzz"
 
32
   (fizzbuzz 15) "fizzbuzz")
 
33
  
 
34
  (test-case
 
35
   "Multiples of 3 are fizz"
 
36
   (check-equal? (fizzbuzz 81) "fizz"))
 
37
  
 
38
  (test-case
 
39
   "Multiples of 5 are buzz"
 
40
   (check-equal? (fizzbuzz 25) "buzz"))
 
41
  
 
42
  (test-equal?
 
43
   "Multiples of 3 and 5 are fizzbuzz"
 
44
   (fizzbuzz 45) "fizzbuzz"))
 
45
 
 
46
(test/gui FizzBuzz)
 
 
b'\\ No newline at end of file'