summaryrefslogtreecommitdiff
path: root/tests/Time_Value_Test.cpp
blob: 95180279f02f12c1178eb1d5680cc994a2c8640b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Time_Value_Test.cpp
//
// = DESCRIPTION
//      This is a simple test of ACE_Time_Value.  No command line arguments
//      are needed to run the test.  It also tests the ACE_U_LongLong class.
//      Because ACE_U_LongLong is used for ACE_hrtime_t on some platforms,
//      this seems like a reasonable place to test it.
//
// = AUTHOR
//    Prashant Jain and David Levine
//
// ============================================================================

// Note, for this test the config.h file *must* come first!
#include "ace/config.h"

// Force test of ACE_U_LongLong class on all platforms except
// ACE_WIN32 and with ACE_HAS_64BIT_LONGS.  ACE_U_LongLong isn't used
// on those platforms.  (And the ACE_U_LongLong declaration is in an
// inconvenient place on ace/OS.h to play this game on ACE_WIN32.)
#if defined (ACE_HAS_HI_RES_TIMER) || \
   (defined (ACE_HAS_LONGLONG_T) && !defined (ACE_HAS_64BIT_LONGS))
# if defined (ACE_HAS_HI_RES_TIMER)
#   undef ACE_HAS_HI_RES_TIMER
# endif /* ACE_HAS_HI_RES_TIMER */

# if defined (ACE_HAS_LONGLONG_T)
#   undef ACE_HAS_LONGLONG_T
# endif /* ACE_HAS_LONGLONG_T */

  // Force inlining, in case ACE_U_LongLong member function
  // definitions are not in libACE.
# define __ACE_INLINE__
# if defined (ACE_NO_INLINE)
#   undef ACE_NO_INLINE
# endif /* ACE_NO_INLINE */
#endif /* ACE_HAS_HI_RES_TIMER || (ACE_HAS_LONGLONG_T && !ACE_HAS_64BIT_LONGS) */

#include "test_config.h"
#include "ace/ACE.h"

#if !defined (ACE_WIN32)  &&  !defined (ACE_HAS_64BIT_LONGS)

static
u_long
check_ace_u_longlong (const char *const name,
                      const ACE_U_LongLong ull,
                      const u_long hi,
                      const u_long lo)
{
  if (ull.hi () == hi  &&  ull.lo () == lo)
    {
      return 0;
    }
  else
    {
      ACE_ERROR ((LM_ERROR, "%s; hi: %x, should be %x; "
                            "lo: %x, should be %x.\n",
                  name, ull.hi (), hi, ull.lo (), lo));
      return 1;
    }
}

static
u_long
test_ace_u_longlong ()
{
  u_long errors = 0;

  ACE_U_LongLong ull1 (0x21,1);
  errors += check_ace_u_longlong ("ull1", ull1, 1, 0x21);

  ACE_U_LongLong ull2 (0x20,2);
  errors += check_ace_u_longlong ("ull2", ull2, 2, 0x20);

  ull2 -= ull1;
  errors += check_ace_u_longlong ("ull2", ull2, 0, 0xfffffffful);

  ull2 += ull1;
  errors += check_ace_u_longlong ("ull2", ull2, 2, 0x20);

  ACE_U_LongLong ull3 = ull1 + ull1;
  errors += check_ace_u_longlong ("ull3", ull3, 2, 0x42);

  ACE_U_LongLong ull4 = ACE_U_LongLong (0x1111, 0) -
                        ACE_U_LongLong (0x1112, 0);
  errors += check_ace_u_longlong ("ull4", ull4, 0xfffffffful, 0xfffffffful);

  ACE_U_LongLong ull5 = ACE_U_LongLong (0x1111, 1) -
                        ACE_U_LongLong (0x1112, 0);
  errors += check_ace_u_longlong ("ull5", ull5, 0, 0xfffffffful);

  ACE_U_LongLong ull6 = ull2 + ACE_U_LongLong (0, 1);
  errors += check_ace_u_longlong ("ull6", ull6, 3, 0x20);

  ull6 += ACE_U_LongLong (0xffffffff, 0xfff0);
  errors += check_ace_u_longlong ("ull6", ull6, 0xfff4, 0x1f);

  return errors;
}
#endif /* ! ACE_WIN32 && ! ACE_HAS_64BIT_LONGS */


int
main (int, char *[])
{
  int ret = 0;

  ACE_START_TEST ("Time_Value_Test");

  ACE_DEBUG ((LM_DEBUG, "This is ACE Version %u.%u.%u\n",
              ACE::major_version (),
              ACE::minor_version(),
              ACE::beta_version()));

  ACE_Time_Value tv1;
  ACE_Time_Value tv2 (2);
  ACE_Time_Value tv3 (100);
  ACE_Time_Value tv4 (1, 1000000);
  ACE_Time_Value tv5 (2);
  ACE_Time_Value tv6 (1, -1000000);
  ACE_Time_Value tv7 ((long) 2.0);

  ACE_ASSERT (tv1 == ACE_Time_Value (0));
  ACE_ASSERT (tv2 < tv3);
  ACE_ASSERT (tv2 <= tv2);
  ACE_ASSERT (tv2 >= tv4);
  ACE_ASSERT (tv5 >= tv6);
  ACE_ASSERT (tv2 == ACE_Time_Value (1, 1000000));
  ACE_ASSERT (tv5 == tv4);
  ACE_ASSERT (tv2 == tv4);
  ACE_ASSERT (tv1 != tv2);
  ACE_ASSERT (tv6 == tv1);
  ACE_ASSERT (tv5 == tv7);

#if !defined (ACE_WIN32)  &&  !defined (ACE_HAS_64BIT_LONGS)
  if (test_ace_u_longlong () != 0) ++ret;
#endif /* ! ACE_WIN32 && ! ACE_HAS_64BIT_LONGS */

  ACE_END_TEST;
  return ret;
}