Skip to content

Instantly share code, notes, and snippets.

@alcidesfp
Last active November 14, 2016 16:22
Show Gist options
  • Save alcidesfp/4724902 to your computer and use it in GitHub Desktop.
Save alcidesfp/4724902 to your computer and use it in GitHub Desktop.
String Calculator en Kawa
; -*- coding: utf-8; mode: Scheme -*- Kawa
;; Coding Kata String Calculator (http://osherove.com/tdd-kata-1/)
(require 'srfi-1 ) ;; List Library
(require 'srfi-13) ;; String Library
(require 'srfi-14) ;; Character-set Library
(define-alias String java.lang.String)
(define-alias RuntimeException java.lang.RuntimeException)
;-------------------------------------------------------------------------
(define (Add input-string :: String) :: int
(let* ((charset-valido (char-set-adjoin char-set:digit #\-))
(lstnum (filter (lambda (n)(<= n 1000))
(map string->number
(string-tokenize input-string
charset-valido))))
(negativos (filter negative? lstnum)))
(if (null? negativos)
(apply + lstnum)
(throw (RuntimeException (format #f
"No se permiten negativos: ~s~%"
negativos))) )))
;-*- coding:utf-8; mode: Scheme -*- Kawa
(load "string_calc.scm")
;; Pruebas Unitarias
(define (assert-equal? exp-val act-val
#!optional (descrip "")(verbose #f))
(let* ((retval (equal? exp-val act-val))
(msgtxt (if (not retval)
(format #f
"~%~a: FAIL exp-val: ~s act-val: ~s~%"
descrip
exp-val
act-val)
(if verbose
(format #f "~a: OK~%" descrip)
"."))))
(display msgtxt)
retval))
;=========================================================================
(define (test-req-1)
"Req 1.1 y 1.2"
(and (assert-equal? 0 (Add ""))
(assert-equal? 1 (Add "1"))
(assert-equal? 3 (Add "1,2"))
(assert-equal? 4 (Add "2,2"))))
;(assert-equal? #t #f "Prueba no implementada")
;-------------------------------------------------------------------------
(define (test-string-calculator)
(let ((mensaje (if (and (test-req-1))
"~%Pruebas SUPERADAS~%"
"~%Pruebas FALLIDAS~%") ))
(format #t mensaje) ))
;-------------------------------------------------------------------------
(test-string-calculator)
;; ;;(test-assert "Prueba no implementada" #f)
;; ;;Req 1.1 y 1.2
;; (test-equal 0 (Add "Hola"))
;; ;;Req 2 y 3
;; (test-equal "Suma '1,2,3'" 6 (Add "1,2,3"))
;; (test-equal "Suma '1\n2,3'" 6 (Add "1\n2,3"))
;; ;;Req 4
;; (test-equal 3 (Add "//;\n1;2"))
;; ;;Req 6, 8 y 9
;; (test-equal 2 (Add "2,1001"))
;; (test-equal 6 (Add "//[*][%]\n1*2%3"))
;; ;; pruebas del script de groovy
;; (test-equal 0 (Add "")) ;;assert 0 == "".sum()
;; (test-equal 0 (Add " ")) ;;assert 0 == " ".sum()
;; (test-equal 1 (Add "1")) ;;assert 1 == "1".sum()
;; (test-equal 1 (Add " 1")) ;;assert 1 == " 1".sum()
;; (test-equal 1 (Add " 1 ")) ;;assert 1 == " 1 ".sum()
;; (test-equal 1 (Add " 1 ")) ;;assert 1 == " 1 ".sum()
;; (test-equal 0 (Add "a")) ;;assert 0 == "a".sum()
;; (test-equal 3 (Add "1,2")) ;;assert 3 == "1,2".sum()
;; (test-equal 1 (Add "1, a")) ;;assert 1 == "1, a".sum()
;; (test-equal 2 (Add "a, 2")) ;;assert 2 == "a, 2".sum()
;; (test-equal 6 (Add "1,2,3")) ;;assert 6 == "1, 2, 3".sum()
;; (test-equal 6 (Add "//;\n1, 2, 3")) ;;assert 6 == "//;\n1, 2, 3".sum()
;; (test-equal 15 (Add "//;'\n1, 2, 3' 4' 5")) ;;assert 15 == "//;'\n1, 2, 3' 4' 5".sum()
;; (test-equal 15 (Add "//;'\n1, 2, 3' 4' 5, \n'")) ;;assert 15 == "//;'\n1, 2, 3' 4' 5, \n'".sum()
;; (test-equal 15 (Add "//;'\n1, 2, 3' 4' 5, \n', a, b; c' d")) ;;assert 15 == "//;'\n1, 2, 3' 4' 5, \n', a, b; c' d".sum()
;; (test-equal 0 (Add ",;\n")) ;;assert 0 == ",;\n".sum()
;; (test-error (Add "-1, 2, 3")) ;;try{ "-1, 2, 3".sum()
;; (test-error (Add "-1, 2, 3, -4, -5")) ;;try{ "-1, 2, 3, -4, -5".sum() }catch(e){ println e}
;; (test-error (Add "//;'\n1, 2, -3' -4' 5")) ;;try{ "//;'\n1, 2, -3' -4' 5".sum() }catch(e){ println e}
;; (test-end "string-calculator")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment