Line data Source code
1 : // Copyright (c) 2024 The Authors. All rights reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : // Authors: liubang (it.liubang@gmail.com)
16 :
17 : #include <gtest/gtest.h>
18 :
19 : #include <climits>
20 :
21 : namespace {
22 : class Solution {
23 : public:
24 3 : int reverse(int x) {
25 3 : long n = 0;
26 19 : while (x != 0) {
27 16 : n = n * 10 + x % 10;
28 16 : x /= 10;
29 : }
30 3 : return n > INT_MAX || n < INT_MIN ? 0 : n;
31 : }
32 : };
33 : } // namespace
34 :
35 4 : TEST(Leetcode, reverse_integer) {
36 1 : Solution s;
37 1 : EXPECT_EQ(321, s.reverse(123));
38 1 : EXPECT_EQ(-321, s.reverse(-123));
39 1 : EXPECT_EQ(0, s.reverse(2147483647));
40 1 : }
|