am 113f7750: am 206fe5fd: Merge changes I1935a63d,I3dfd3647

* commit '113f77506b2382eeb2d99c74f84579c59832fbbb':
  Fix and clean up strtotimeval
  Added strtotimeval tests.
This commit is contained in:
Calin Juravle 2014-03-27 17:06:14 +00:00 committed by Android Git Automerger
commit 4f6cd7cf9b
2 changed files with 68 additions and 25 deletions

View File

@ -30,34 +30,29 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
char * strtotimeval (const char *str, struct timeval *ts) char * strtotimeval(const char *str, struct timeval *ts) {
{ char *s;
int n; long fs = 0; /* fractional seconds */
char *s, *s0;
long fs; /* Fractional seconds */
ts->tv_sec = strtoumax(str, &s, 10); ts->tv_sec = strtoumax(str, &s, 10);
fs = 0;
if ( *s == '.' ) { if (*s == '.') {
int count; s++;
int count = 0;
s0 = s+1; /* read up to 6 digits (microseconds) */
while (*s && isdigit(*s)) {
/* read up to 6 digits */ if (++count < 7) {
fs = 0; fs = fs*10 + (*s - '0');
count = 0; }
while ( *s && isdigit(*s) ) s++;
{
if ( ++count < 7 )
fs = fs*10 + (*s - '0');
s++;
}
for ( ; count < 6; count++ )
fs *= 10;
} }
ts->tv_usec = fs; for (; count < 6; count++) {
return s; fs *= 10;
}
}
ts->tv_usec = fs;
return s;
} }

View File

@ -387,3 +387,51 @@ TEST(time, timer_delete_from_timer_thread) {
ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id)); ASSERT_EQ(ESRCH, pthread_detach(tdd.thread_id));
#endif #endif
} }
TEST(time, strtotimeval) {
#if defined(__BIONIC__)
struct timeval tv1;
char* rest1 = strtotimeval("10.123456", &tv1);
ASSERT_EQ(10, tv1.tv_sec);
ASSERT_EQ(123456, tv1.tv_usec);
ASSERT_EQ('\0', *rest1);
// strtotimeval interprets the fractional part as microseconds and thus will
// only consider its first 6 digits. Even so it should consume all valid
// digits.
struct timeval tv2;
char* rest2 = strtotimeval(".1234567", &tv2);
ASSERT_EQ(0, tv2.tv_sec);
ASSERT_EQ(123456, tv2.tv_usec);
ASSERT_EQ('\0', *rest2);
struct timeval tv3;
char* rest3 = strtotimeval("1.1a", &tv3);
ASSERT_EQ(1, tv3.tv_sec);
ASSERT_EQ(100000, tv3.tv_usec);
ASSERT_EQ('a', *rest3);
struct timeval tv4;
char* rest4 = strtotimeval("a", &tv4);
ASSERT_EQ(0, tv4.tv_sec);
ASSERT_EQ(0, tv4.tv_usec);
ASSERT_EQ('a', *rest4);
struct timeval tv5;
char* rest5 = strtotimeval("0", &tv5);
ASSERT_EQ(0, tv5.tv_sec);
ASSERT_EQ(0, tv5.tv_usec);
ASSERT_EQ('\0', *rest5);
// TODO: should we reject this case and just return '.'?
struct timeval tv6;
char* rest6 = strtotimeval(".", &tv6);
ASSERT_EQ(0, tv6.tv_sec);
ASSERT_EQ(0, tv6.tv_usec);
ASSERT_EQ('\0', *rest6);
#else // __BIONIC__
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif // __BIONIC__
}