tTimeVal.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. /* ====================================================================
  2.  * The Vovida Software License, Version 1.0 
  3.  * 
  4.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  * 
  18.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  19.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  20.  *    not be used to endorse or promote products derived from this
  21.  *    software without prior written permission. For written
  22.  *    permission, please contact vocal@vovida.org.
  23.  *
  24.  * 4. Products derived from this software may not be called "VOCAL", nor
  25.  *    may "VOCAL" appear in their name, without prior written
  26.  *    permission of Vovida Networks, Inc.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  31.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  32.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  33.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40.  * DAMAGE.
  41.  * 
  42.  * ====================================================================
  43.  * 
  44.  * This software consists of voluntary contributions made by Vovida
  45.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  46.  * Inc.  For more information on Vovida Networks, Inc., please see
  47.  * <http://www.vovida.org/>.
  48.  *
  49.  */
  50. static const char* const tTimeVal_cxx_Version = 
  51.   "$Id: tTimeVal.cxx,v 1.7 2001/04/10 02:12:51 icahoon Exp $";
  52. #include "Application.hxx"
  53. #include "TimeVal.hxx"
  54. #include "Writer.hxx"
  55. #include "Condition.hxx"
  56. using namespace Vocal;
  57. using namespace Vocal::TimeAndDate;
  58. using namespace Vocal::Threads;
  59. class TestApplication : public Application
  60. {
  61.     public:
  62.      ReturnCode   run();
  63. };
  64. Application * Application::create()
  65. {
  66.     return ( new TestApplication );
  67. }
  68. int main(int argc, char ** argv, char ** arge)
  69. {
  70.     return ( Application::main(argc, argv, arge) );
  71. }
  72. ReturnCode TestApplication::run()
  73. {
  74.     TimeVal  sec0, sec5, sec10, sec15, sec20;
  75.     cout << "sec0:  " << sec0  << ", ms = " << sec0.milliseconds()  << endl
  76.       << "sec5:  " << sec5  << ", ms = " << sec5.milliseconds()  << endl
  77.       << "sec10: " << sec10 << ", ms = " << sec10.milliseconds() << endl
  78.       << "sec15: " << sec15 << ", ms = " << sec15.milliseconds() << endl
  79.       << "sec20: " << sec20 << ", ms = " << sec20.milliseconds() << endl;
  80.     sec0.now();
  81.     
  82.     for ( int i = 0; i < 1; i++ ) { usleep(1000000); cout << '.'; } cout << endl;
  83.     sec5.now();
  84.     for ( int i = 0; i < 1; i++ ) { usleep(1000000); cout << '.'; } cout << endl;
  85.     sec10.now();
  86.     
  87.     for ( int i = 0; i < 1; i++ ) { usleep(1000000); cout << '.'; } cout << endl;
  88.     sec15.now();
  89.     for ( int i = 0; i < 1; i++ ) { usleep(1000000); cout << '.'; } cout << endl;
  90.     sec20.now();
  91.     cout << "sec0:  " << sec0  << ", ms = " << sec0.milliseconds()  << endl
  92.       << "sec5:  " << sec5  << ", ms = " << sec5.milliseconds()  << endl
  93.       << "sec10: " << sec10 << ", ms = " << sec10.milliseconds() << endl
  94.       << "sec15: " << sec15 << ", ms = " << sec15.milliseconds() << endl
  95.       << "sec20: " << sec20 << ", ms = " << sec20.milliseconds() << endl;
  96.     cout << "sec 5 - sec0:   " << sec5.milliseconds()  - sec0.milliseconds()  << endl
  97.       << "sec 10 - sec5:  " << sec10.milliseconds() - sec5.milliseconds()  << endl
  98.       << "sec 15 - sec10: " << sec15.milliseconds() - sec10.milliseconds() << endl
  99.       << "sec 20 - sec15: " << sec20.milliseconds() - sec15.milliseconds() << endl;
  100.     timeval tv;
  101.     
  102.     tv.tv_sec = 5;
  103.     tv.tv_usec = 5000000;
  104.     
  105.     cout << "tv = " << TimeVal(tv) << endl;
  106.     cout << ( sec0 + tv ) << endl;
  107.     
  108.     sec5 += tv;
  109.     sec10 += 10000;
  110.     TimeVal  d0, d5, d10, d15;
  111.     
  112.     d0 = sec5 - sec0;
  113.     d5 = sec10 - sec5;
  114.     d10 = sec15 - sec10;
  115.     d15 = sec20 - sec15;
  116.     cout << "sec0:  " << sec0  << ", ms = " << sec0.milliseconds()  << endl
  117.       << "sec5:  " << sec5  << ", ms = " << sec5.milliseconds()  << endl
  118.       << "sec10: " << sec10 << ", ms = " << sec10.milliseconds() << endl
  119.       << "sec15: " << sec15 << ", ms = " << sec15.milliseconds() << endl
  120.       << "sec20: " << sec20 << ", ms = " << sec20.milliseconds() << endl;
  121.     cout << "sec5 - sec0:   " << sec5.milliseconds()  - sec0.milliseconds()  << endl
  122.       << "sec10 - sec5:  " << sec10.milliseconds() - sec5.milliseconds()  << endl
  123.       << "sec15 - sec10: " << sec15.milliseconds() - sec10.milliseconds() << endl
  124.       << "sec20 - sec15: " << sec20.milliseconds() - sec15.milliseconds() << endl;
  125.     cout << "d0:  " << d0  << ", ms = " << d0.milliseconds()  << endl
  126.       << "d5:  " << d5  << ", ms = " << d5.milliseconds()  << endl
  127.       << "d10: " << d10 << ", ms = " << d10.milliseconds() << endl
  128.       << "d15: " << d15 << ", ms = " << d15.milliseconds() << endl;
  129.     cout << "sec5 - sec0:   " << d0.milliseconds()  << endl
  130.       << "sec10 - sec5:  " << d5.milliseconds()  << endl
  131.       << "sec15 - sec10: " << d10.milliseconds() << endl
  132.       << "sec20 - sec15: " << d15.milliseconds() << endl;
  133.     cout << "sec0 < sec5 ? " << ( sec0 < sec5 ? "true" : "false" ) << endl;
  134.     cout << "sec0 == sec5 ? " << ( sec0 == sec5 ? "true" : "false" ) << endl;
  135.     cout << "sec20 == sec20 ? " << ( sec20 == sec20 ? "true" : "false" ) << endl;
  136.     timeval tv0;
  137.     tv0.tv_sec = -10;
  138.     tv0.tv_usec = -10000000;
  139.     
  140.     cout << "tv0: " << TimeVal(tv0) << endl;    
  141.     timeval tv1;
  142.     tv1.tv_sec = -9;
  143.     tv1.tv_usec = -10000001;
  144.     
  145.     cout << "tv1: " << TimeVal(tv1) << endl;    
  146.     timeval tv2;
  147.     tv2.tv_sec = -9;
  148.     tv2.tv_usec = -11000000;
  149.     
  150.     cout << "tv2: " << TimeVal(tv2) << endl;    
  151.     cout << "tv1 < tv0 ? " 
  152.       << ( (TimeVal(tv1) < TimeVal(tv0)) ? "true" : "false" ) << endl;
  153.     cout << "tv2 == tv0 ? " 
  154.       << ( (TimeVal(tv2) == TimeVal(tv0)) ? "true" : "false" ) << endl;
  155.     TimeVal foo, bar;
  156.     foo.now();
  157.     for ( size_t i = 0; i < 1; i++ )
  158.     {
  159.         vusleep(1000);
  160.     }
  161.     bar.now();
  162.     
  163.     TimeVal diff = bar - foo;
  164.     cout << " bar - foo = " << diff.milliseconds() << endl;
  165.         
  166.     return ( SUCCESS );
  167. }