Line data Source code
1 : // Copyright (c) 2022 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 : // Created: 2022/01/17 14:50
17 :
18 : #include <gtest/gtest.h>
19 :
20 : #include <climits>
21 :
22 : namespace {
23 : class Solution {
24 : public:
25 3 : int reverse(int x) {
26 3 : long n = 0;
27 19 : while (x != 0) {
28 16 : n = n * 10 + x % 10;
29 16 : x /= 10;
30 : }
31 3 : return n > INT_MAX || n < INT_MIN ? 0 : n;
32 : }
33 : };
34 : } // namespace
35 :
36 4 : TEST(Leetcode, reverse_integer) {
37 1 : Solution s;
38 1 : EXPECT_EQ(321, s.reverse(123));
39 1 : EXPECT_EQ(-321, s.reverse(-123));
40 1 : EXPECT_EQ(0, s.reverse(2147483647));
41 1 : }
|