]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte_os/tests/test_time/fosa_test_time.c
Renaming fosa/src_marte to fosa/src_marte_os
[frescor/fosa.git] / src_marte_os / tests / test_time / fosa_test_time.c
diff --git a/src_marte_os/tests/test_time/fosa_test_time.c b/src_marte_os/tests/test_time/fosa_test_time.c
new file mode 100644 (file)
index 0000000..9aebc13
--- /dev/null
@@ -0,0 +1,293 @@
+// -----------------------------------------------------------------------
+//  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
+//
+//    Universidad de Cantabria,              SPAIN
+//    University of York,                    UK
+//    Scuola Superiore Sant'Anna,            ITALY
+//    Kaiserslautern University,             GERMANY
+//    Univ. Politécnica  Valencia,           SPAIN
+//    Czech Technical University in Prague,  CZECH REPUBLIC
+//    ENEA                                   SWEDEN
+//    Thales Communication S.A.              FRANCE
+//    Visual Tools S.A.                      SPAIN
+//    Rapita Systems Ltd                     UK
+//    Evidence                               ITALY
+//
+//    See http://www.frescor.org for a link to partners' websites
+//
+//           FRESCOR project (FP6/2005/IST/5-034026) is funded
+//        in part by the European Union Sixth Framework Programme
+//        The European Union is not liable of any use that may be
+//        made of this code.
+//
+//
+//  based on previous work (FSF) done in the FIRST project
+//
+//   Copyright (C) 2005  Mälardalen University, SWEDEN
+//                       Scuola Superiore S.Anna, ITALY
+//                       Universidad de Cantabria, SPAIN
+//                       University of York, UK
+//
+//   FSF API web pages: http://marte.unican.es/fsf/docs
+//                      http://shark.sssup.it/contrib/first/docs/
+//
+//   This file is part of FOSA (Frsh Operating System Adaption)
+//
+//  FOSA is free software; you can redistribute it and/or modify it
+//  under terms of the GNU General Public License as published by the
+//  Free Software Foundation; either version 2, or (at your option) any
+//  later version.  FOSA is distributed in the hope that it will be
+//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//  General Public License for more details. You should have received a
+//  copy of the GNU General Public License along with FOSA; see file
+//  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
+//  Cambridge, MA 02139, USA.
+//
+//  As a special exception, including FOSA header files in a file,
+//  instantiating FOSA generics or templates, or linking other files
+//  with FOSA objects to produce an executable application, does not
+//  by itself cause the resulting executable application to be covered
+//  by the GNU General Public License. This exception does not
+//  however invalidate any other reasons why the executable file might be
+//  covered by the GNU Public License.
+// -----------------------------------------------------------------------
+#include "fosa.h"
+#include <assert.h>
+#include <time.h>   /* for timespec */
+#include <stdio.h>  /* for printf */
+#include <string.h> /* for memcmp */
+
+static int check_exact_abs_msec(fosa_abs_time_t abs_time, long msec);
+static int check_exact_rel_msec(fosa_rel_time_t rel_time, long msec);
+
+int main()
+{
+
+    struct timespec tspec1 = {1, 500000000};
+    struct timespec tspec2 = {0, 400000000};
+    struct timespec tspec_result;
+
+    fosa_rel_time_t rel_time1 = fosa_msec_to_rel_time(1500);
+    fosa_rel_time_t rel_time2 = fosa_msec_to_rel_time(400);
+    fosa_rel_time_t rel_time_aux = fosa_msec_to_rel_time(1500);
+    fosa_rel_time_t rel_time_result;
+
+    fosa_abs_time_t abs_time1 = fosa_msec_to_abs_time(1500);
+    fosa_abs_time_t abs_time2 = fosa_msec_to_abs_time(400);
+    fosa_abs_time_t abs_time_aux = fosa_msec_to_abs_time(1500);
+    fosa_abs_time_t abs_time_result;
+
+    long numeric;
+    bool test;
+
+    /* fosa_abs_time_incr */
+    abs_time_result = fosa_abs_time_incr(abs_time1, rel_time2);
+    assert( check_exact_abs_msec(abs_time_result, 1900) );
+
+    /* fosa_abs_time_decr */
+    abs_time_result = fosa_abs_time_decr(abs_time1, rel_time2);
+    assert( check_exact_abs_msec(abs_time_result, 1100) );
+
+    /* fosa_abs_time_decr (negative) */
+    abs_time_result = fosa_abs_time_decr(abs_time2, rel_time1);
+//    assert( check_exact_abs_msec(abs_time_result, -1100) );
+
+    /* fosa_abs_time_extract_interval */
+    rel_time_result = fosa_abs_time_extract_interval(abs_time2, abs_time1);
+    assert( check_exact_rel_msec(rel_time_result, 1100) );
+
+    /* fosa_abs_time_extract_interval (negative) */
+    rel_time_result = fosa_abs_time_extract_interval(abs_time1, abs_time2);
+//    assert( check_exact_rel_msec(rel_time_result, -1100) );
+
+    /* fosa_rel_time_add */
+    rel_time_result = fosa_rel_time_add(rel_time1, rel_time2);
+    assert( check_exact_rel_msec(rel_time_result, 1900) );
+    
+    /* fosa_rel_time_decr */
+    rel_time_result = fosa_rel_time_decr(rel_time1, rel_time2);
+    assert( check_exact_rel_msec(rel_time_result, 1100) );
+    
+    /* fosa_rel_time_decr (negative) */
+    rel_time_result = fosa_rel_time_decr(rel_time2, rel_time1);
+//    assert( check_exact_rel_msec(rel_time_result, 1100) );
+
+/*
+    struct timespec t1 = {0, 876352172};
+    fosa_rel_time_t t1_rel = fosa_timespec_to_rel_time(t1);
+    long factor = 573102543;
+    struct timespec product = {502239658, 336773396};
+    fosa_rel_time_t product_rel = fosa_timespec_to_rel_time(product);
+*/
+
+    struct timespec t1 = {0, 876352172};
+    struct timespec product = {50223965,  570771688};
+    long factor = 57310254;
+    struct timespec product_divided_by_17 = {2954350, 915927746};  // inexact division
+    struct timespec product_divided_by_3 = {16741321, 856923896};  // exact division
+
+    fosa_rel_time_t t1_rel = fosa_timespec_to_rel_time(t1);
+    fosa_rel_time_t product_rel = fosa_timespec_to_rel_time(product);
+    fosa_rel_time_t product_17_rel = fosa_timespec_to_rel_time(product_divided_by_17);
+    fosa_rel_time_t product_3_rel = fosa_timespec_to_rel_time(product_divided_by_3);
+
+    /* fosa_rel_time_times_integer */
+    /* fosa_rel_time_divided_by_integer */
+    rel_time_result = fosa_rel_time_times_integer(rel_time2, 4);
+    assert(  check_exact_rel_msec(rel_time_result, 1600) );
+
+    rel_time_result = fosa_rel_time_divided_by_integer(rel_time_result, 4);
+    assert(  check_exact_rel_msec(rel_time_result, 400) );
+
+    rel_time_result = fosa_rel_time_times_integer(t1_rel, factor);
+    assert(  fosa_rel_time_equal(rel_time_result, product_rel) );
+
+    rel_time_result = fosa_rel_time_divided_by_integer(product_rel, factor);
+    assert(  fosa_rel_time_equal(rel_time_result, t1_rel) );
+    
+    rel_time_result = fosa_rel_time_divided_by_integer(product_rel, 17);
+    assert(  fosa_rel_time_equal(rel_time_result, product_17_rel) );
+    
+    rel_time_result = fosa_rel_time_divided_by_integer(product_rel, 3);
+    assert(  fosa_rel_time_equal(rel_time_result, product_3_rel) );
+    
+    /* fosa_abs_time_smaller */
+    test = fosa_abs_time_smaller(abs_time1, abs_time2);
+    assert(test == false);
+
+    test = fosa_abs_time_smaller(abs_time1, abs_time_aux);
+    assert(test == false);
+
+    test = fosa_abs_time_smaller(abs_time2, abs_time1);
+    assert(test);
+
+    /* fosa_rel_time_smaller */
+    test = fosa_rel_time_smaller(rel_time1, rel_time2);
+    assert(test == false);
+
+    test = fosa_rel_time_smaller(rel_time1, rel_time_aux);
+    assert(test == false);
+
+    test = fosa_rel_time_smaller(rel_time2, rel_time1);
+    assert(test);
+
+    /* fosa_abs_time_smaller_or_equal */
+    test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time2);
+    assert(test == false);
+
+    test = fosa_abs_time_smaller_or_equal(abs_time1, abs_time_aux);
+    assert(test);
+
+    test = fosa_abs_time_smaller_or_equal(abs_time2, abs_time1);
+    assert(test);
+
+    /* fosa_rel_time_smaller_or_equal */
+    test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time2);
+    assert(test == false);
+
+    test = fosa_rel_time_smaller_or_equal(rel_time1, rel_time_aux);
+    assert(test);
+
+    test = fosa_rel_time_smaller_or_equal(rel_time2, rel_time1);
+    assert(test);
+
+    /* fosa_msec_to_rel_time */
+    rel_time_result = fosa_msec_to_rel_time(1850);
+    assert( check_exact_rel_msec(rel_time_result, 1850) );
+
+    /* fosa_rel_time_to_msec */
+    numeric = fosa_rel_time_to_msec(rel_time1);
+    assert(numeric == 1500);
+
+    /* fosa_msec_to_abs_time */
+    abs_time_result = fosa_msec_to_rel_time(1320);
+    assert( check_exact_abs_msec(abs_time_result, 1320) );
+
+    /* fosa_abs_time_to_msec */
+    numeric = fosa_abs_time_to_msec(abs_time2);
+    assert(numeric == 400);
+
+    /* fosa_usec_to_rel_time */
+    rel_time_result = fosa_usec_to_rel_time(1850000);
+    assert( check_exact_rel_msec(rel_time_result, 1850) );
+
+    /* fosa_rel_time_to_usec */
+    numeric = fosa_rel_time_to_usec(rel_time1);
+    assert(numeric == 1500000);
+
+    /* fosa_msec_to_abs_time */
+    abs_time_result = fosa_usec_to_rel_time(1320000);
+    assert( check_exact_abs_msec(abs_time_result, 1320) );
+
+    /* fosa_abs_time_to_msec */
+    numeric = fosa_abs_time_to_usec(abs_time2);
+    assert(numeric == 400000);
+
+    /* fosa_timespec_to_rel_time */
+    struct timespec tspec_aux = {1, 750000000};
+    rel_time_result = fosa_timespec_to_rel_time(tspec_aux);
+    assert( check_exact_rel_msec(rel_time_result, 1750) );
+
+    /* fosa_rel_time_to_timespec */
+    tspec_result = fosa_rel_time_to_timespec(rel_time1);
+    test = memcmp(&tspec_result, &tspec1, sizeof(tspec_result) );
+    assert(test == 0);
+
+    /* fosa_timespec_to_abs_time */
+    struct timespec tspec_aux_extra = {1, 200000000};
+    abs_time_result = fosa_timespec_to_abs_time(tspec_aux_extra);
+    assert( check_exact_abs_msec(abs_time_result, 1200) );
+
+    /* fosa_abs_time_to_timespec */
+    tspec_result = fosa_abs_time_to_timespec(abs_time2);
+    test = memcmp(&tspec_result, &tspec2, sizeof(tspec_result) );
+    assert(test == 0);
+
+
+
+    printf("End of test OK!!\n");
+
+    return 0;
+}
+
+// -------------------------------------------------------------
+
+static int check_exact_abs_msec(fosa_abs_time_t abs_time, long msec)
+{
+    fosa_abs_time_t time_check;
+    int cmp = -1;
+
+    time_check = fosa_msec_to_abs_time(msec);
+    cmp = memcmp(&time_check, &abs_time, sizeof(time_check) );
+
+    if (cmp == 0)
+    {
+        return 1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+// -------------------------------------------------------------
+
+static int check_exact_rel_msec(fosa_rel_time_t rel_time, long msec)
+{
+    fosa_rel_time_t time_check;
+    int cmp = -1;
+
+    time_check = fosa_msec_to_rel_time(msec);
+    cmp = memcmp(&time_check, &rel_time, sizeof(time_check) );
+
+    if (cmp == 0)
+    {
+        return 1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+