Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
338ef4aa
Commit
338ef4aa
authored
9 years ago
by
Jan Tattermusch
Browse files
Options
Downloads
Patches
Plain Diff
fix bug MIN_VALUE bug in ltoa and int64toa and add tests
parent
f67f071f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/core/support/string.c
+18
-8
18 additions, 8 deletions
src/core/support/string.c
test/core/support/string_test.c
+49
-0
49 additions, 0 deletions
test/core/support/string_test.c
with
67 additions
and
8 deletions
src/core/support/string.c
+
18
−
8
View file @
338ef4aa
...
@@ -153,6 +153,7 @@ void gpr_reverse_bytes(char *str, int len) {
...
@@ -153,6 +153,7 @@ void gpr_reverse_bytes(char *str, int len) {
}
}
int
gpr_ltoa
(
long
value
,
char
*
string
)
{
int
gpr_ltoa
(
long
value
,
char
*
string
)
{
unsigned
long
uval
;
int
i
=
0
;
int
i
=
0
;
int
neg
=
value
<
0
;
int
neg
=
value
<
0
;
...
@@ -162,10 +163,14 @@ int gpr_ltoa(long value, char *string) {
...
@@ -162,10 +163,14 @@ int gpr_ltoa(long value, char *string) {
return
1
;
return
1
;
}
}
if
(
neg
)
value
=
-
value
;
if
(
neg
)
{
while
(
value
)
{
uval
=
-
value
;
string
[
i
++
]
=
(
char
)(
'0'
+
value
%
10
);
}
else
{
value
/=
10
;
uval
=
value
;
}
while
(
uval
)
{
string
[
i
++
]
=
(
char
)(
'0'
+
uval
%
10
);
uval
/=
10
;
}
}
if
(
neg
)
string
[
i
++
]
=
'-'
;
if
(
neg
)
string
[
i
++
]
=
'-'
;
gpr_reverse_bytes
(
string
,
i
);
gpr_reverse_bytes
(
string
,
i
);
...
@@ -174,6 +179,7 @@ int gpr_ltoa(long value, char *string) {
...
@@ -174,6 +179,7 @@ int gpr_ltoa(long value, char *string) {
}
}
int
gpr_int64toa
(
gpr_int64
value
,
char
*
string
)
{
int
gpr_int64toa
(
gpr_int64
value
,
char
*
string
)
{
gpr_uint64
uval
;
int
i
=
0
;
int
i
=
0
;
int
neg
=
value
<
0
;
int
neg
=
value
<
0
;
...
@@ -183,10 +189,14 @@ int gpr_int64toa(gpr_int64 value, char *string) {
...
@@ -183,10 +189,14 @@ int gpr_int64toa(gpr_int64 value, char *string) {
return
1
;
return
1
;
}
}
if
(
neg
)
value
=
-
value
;
if
(
neg
)
{
while
(
value
)
{
uval
=
-
value
;
string
[
i
++
]
=
(
char
)(
'0'
+
value
%
10
);
}
else
{
value
/=
10
;
uval
=
value
;
}
while
(
uval
)
{
string
[
i
++
]
=
(
char
)(
'0'
+
uval
%
10
);
uval
/=
10
;
}
}
if
(
neg
)
string
[
i
++
]
=
'-'
;
if
(
neg
)
string
[
i
++
]
=
'-'
;
gpr_reverse_bytes
(
string
,
i
);
gpr_reverse_bytes
(
string
,
i
);
...
...
This diff is collapsed.
Click to expand it.
test/core/support/string_test.c
+
49
−
0
View file @
338ef4aa
...
@@ -286,6 +286,53 @@ static void test_strsplit(void) {
...
@@ -286,6 +286,53 @@ static void test_strsplit(void) {
gpr_free
(
parts
);
gpr_free
(
parts
);
}
}
test_ltoa
()
{
char
*
str
;
char
buf
[
GPR_LTOA_MIN_BUFSIZE
];
LOG_TEST_NAME
(
"test_ltoa"
);
/* zero */
GPR_ASSERT
(
1
==
gpr_ltoa
(
0
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"0"
,
buf
));
/* positive number */
GPR_ASSERT
(
3
==
gpr_ltoa
(
123
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"123"
,
buf
));
/* negative number */
GPR_ASSERT
(
6
==
gpr_ltoa
(
-
12345
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"-12345"
,
buf
));
/* large negative - we don't know the size of long in advance */
GPR_ASSERT
(
gpr_asprintf
(
&
str
,
"%lld"
,
(
long
long
)
LONG_MIN
));
GPR_ASSERT
(
strlen
(
str
)
==
gpr_ltoa
(
LONG_MIN
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
str
,
buf
));
gpr_free
(
str
);
}
test_int64toa
()
{
char
buf
[
GPR_INT64TOA_MIN_BUFSIZE
];
LOG_TEST_NAME
(
"test_int64toa"
);
/* zero */
GPR_ASSERT
(
1
==
gpr_int64toa
(
0
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"0"
,
buf
));
/* positive */
GPR_ASSERT
(
3
==
gpr_int64toa
(
123
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"123"
,
buf
));
/* large positive */
GPR_ASSERT
(
19
==
gpr_int64toa
(
9223372036854775807LL
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"9223372036854775807"
,
buf
));
/* large negative */
GPR_ASSERT
(
20
==
gpr_int64toa
(
-
9223372036854775808LL
,
buf
));
GPR_ASSERT
(
0
==
strcmp
(
"-9223372036854775808"
,
buf
));
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
grpc_test_init
(
argc
,
argv
);
grpc_test_init
(
argc
,
argv
);
test_strdup
();
test_strdup
();
...
@@ -296,5 +343,7 @@ int main(int argc, char **argv) {
...
@@ -296,5 +343,7 @@ int main(int argc, char **argv) {
test_strjoin
();
test_strjoin
();
test_strjoin_sep
();
test_strjoin_sep
();
test_strsplit
();
test_strsplit
();
test_ltoa
();
test_int64toa
();
return
0
;
return
0
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment