Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "CUnit/Basic.h"
- int min(int a, int b);
- int max(int a, int b);
- int succ(int a); // incrementa a di uno
- int pred(int a); // decrementa a di uno
- int sum(int a, int b);
- int subtract(int a, int b);
- int product(int a, int b);
- int factorial(int n, int fatt); // 0 <= n <= 10
- int min(int a, int b) {
- if (a < b) {
- return a;
- }
- if (a > b) {
- return b;
- }
- if (a == b) {
- return "Impossibile calcolare il minimo tra questi due numeri";
- }
- }
- int max(int a, int b) {
- if (a > b) {
- return a;
- }
- if (b > a) {
- return b;
- }
- if (a == b) {
- return "Impossibile calcolare il massimo tra questi due numeri";
- }
- }
- int succ(int a) {
- a = a + 1;
- return a;
- }
- int pred(int a) {
- a = a - 1;
- return a;
- }
- int sum(int a, int b) {
- int i, s = a;
- if (b > 0 && a > 0) {
- for (i = 0; i < b; i++) {
- s = succ(s);
- }
- }
- if (b < 0 && a > 0) {
- for (i = b; i < 0; i++) {
- s = pred(s);
- }
- }
- if (b < 0 && a < 0) {
- for (i = b; i < 0; i++) {
- s = pred(s);
- }
- }
- if (b > 0 && a < 0) {
- for (i = 0; i < b; i++) {
- s = succ(s);
- }
- }
- if (b != 0 && a == 0) {
- return b;
- }
- return s;
- }
- int subtract(int a, int b) {
- int i, s = a;
- if (b != 0 && a == 0) {
- return -b;
- }
- if (b == 0 && a != 0) {
- return s;
- }
- if (a > 0 && b > 0) {
- for (i = 0; i < b; i++) {
- s = pred(s);
- }
- return s;
- }
- if (a < 0 && b < 0) {
- for (i = b; i < 0; i++) {
- s = succ(s);
- }
- return s;
- }
- if (a < 0 && b > 0) {
- for (i = 0; i < b; i++) {
- s = pred(s);
- }
- return s;
- }
- if (a > 0 && b < 0) {
- for (i = b; i < 0; i++) {
- s = succ(s);
- }
- return s;
- }
- }
- int product(int a, int b) {
- int i, s = 0;
- if (a > 0 && b > 0) {
- for (i = 0; i < b; i++) {
- s = sum(a, s);
- }
- return s;
- }
- if (a > 0 && b < 0) {
- for (i = b; i < 0; i++) {
- s = sum(a, s);
- }
- return -s;
- }
- if (a < 0 && b < 0) {
- for (i = b; i < 0; i++) {
- s = sum(a, s);
- }
- return s *-1;
- }
- if (a < 0 && b > 0) {
- for (i = 0; i < b; i++) {
- s = sum(a, s);
- }
- return s;
- }
- if (a == 0 || b == 0) {
- return 0;
- }
- }
- int factorial(int n, int fatt){
- int i;
- if(n < 0){
- return "Impossibile calcolare il fattoriale di un numero negativo";
- }
- if(n == 0){
- return 0;
- }
- if(n > 0){
- //for(i = 1; i <= n; i++){
- //fatt = fatt * i;
- //}
- if(n == 1){
- return fatt;
- }
- if(n > 1){
- fatt = product(fatt, n);
- n--;
- return factorial(n, fatt);
- }
- }
- }
- /*
- * Aggiungere tutti i metodi di test per le funzioni da testare
- */
- void test_min(void) {
- CU_ASSERT_EQUAL( min(4, 10), 4);
- CU_ASSERT_STRING_EQUAL(min(2,2), "Impossibile calcolare il minimo tra questi due numeri" );
- }
- void test_max(void) {
- CU_ASSERT_EQUAL(max(10, 11), 11);
- CU_ASSERT_EQUAL(max(10, 10), "Impossibile calcolare il massimo tra questi due numeri");
- CU_ASSERT_EQUAL(max(0, 0), "Impossibile calcolare il massimo tra questi due numeri");
- }
- void test_sum(void) {
- CU_ASSERT_EQUAL(sum(5, 6), 11);
- CU_ASSERT_EQUAL(sum(4, -3), 1);
- CU_ASSERT_EQUAL(sum(-4, -3), -7);
- CU_ASSERT_EQUAL(sum(-4, 3), -1);
- CU_ASSERT_EQUAL(sum(4, 0), 4);
- CU_ASSERT_EQUAL(sum(0, -3), -3);
- }
- void test_pred(void) {
- CU_ASSERT_EQUAL(succ(2), 3);
- }
- void test_succ(void) {
- CU_ASSERT_EQUAL(pred(2), 1);
- }
- void test_subtract(void) {
- CU_ASSERT_EQUAL(subtract(5, 6), -1);
- CU_ASSERT_EQUAL(subtract(4, -3), 7);
- CU_ASSERT_EQUAL(subtract(-4, -3), -1);
- CU_ASSERT_EQUAL(subtract(-4, 3), -7);
- CU_ASSERT_EQUAL(subtract(4, 0), 4);
- CU_ASSERT_EQUAL(subtract(0, 3), -3);
- }
- void test_product(void) {
- CU_ASSERT_EQUAL(product(2, 3), 6);
- CU_ASSERT_EQUAL(product(2, -3), -6);
- CU_ASSERT_EQUAL(product(-2, -3), 6);
- CU_ASSERT_EQUAL(product(-2, 3), -6);
- CU_ASSERT_EQUAL(product(0, 3), 0);
- CU_ASSERT_EQUAL(product(-2, 0), 0);
- }
- void test_factorial(void){
- CU_ASSERT_EQUAL(factorial(3, 1), 6);
- CU_ASSERT_STRING_EQUAL(factorial(-3, 1), "Impossibile calcolare il fattoriale di un numero negativo");
- CU_ASSERT_EQUAL(factorial(0, 1), 0);
- CU_ASSERT_EQUAL(factorial(10, 1), 3628800);
- }
- /*
- * Funzioni di inizializzazione e pulizia delle suite.
- * Di default sono funzioni vuote.
- */
- int init_suite_default(void) {
- return 0;
- }
- int clean_suite_default(void) {
- return 0;
- }
- /* **************************************************
- * TEST di UNITA'
- */
- int main() {
- /* inizializza registro - e' la prima istruzione */
- CU_initialize_registry();
- /* Aggiungi le suite al test registry */
- CU_pSuite pSuite_A = CU_add_suite("Suite_A", init_suite_default,
- clean_suite_default);
- CU_pSuite pSuite_B = CU_add_suite("Suite_B", init_suite_default,
- clean_suite_default);
- CU_pSuite pSuite_C = CU_add_suite("Suite_C", init_suite_default,
- clean_suite_default);
- CU_pSuite pSuite_D = CU_add_suite("Suite_D", init_suite_default,
- clean_suite_default);
- CU_pSuite pSuite_E = CU_add_suite("Suite_E", init_suite_default,
- clean_suite_default);
- /* Aggiungi i test alle suite
- * NOTA - L'ORDINE DI INSERIMENTO E' IMPORTANTE
- */
- CU_add_test(pSuite_A, "test of min()", test_min);
- CU_add_test(pSuite_A, "test of max()", test_max);
- CU_add_test(pSuite_B, "test of sum()", test_sum);
- CU_add_test(pSuite_C, "test of pred()", test_pred);
- CU_add_test(pSuite_C, "test of succ()", test_succ);
- CU_add_test(pSuite_D, "test of subtract()", test_subtract);
- CU_add_test(pSuite_D, "test of product()", test_product);
- CU_add_test(pSuite_E, "test of factorial()", test_factorial);
- /* Esegue tutti i casi di test con output sulla console */
- CU_basic_set_mode(CU_BRM_VERBOSE);
- CU_basic_run_tests();
- /* Pulisce il registro e termina lo unit test */
- CU_cleanup_registry();
- system("PAUSE");
- return CU_get_error();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement