1 // Copyright 2016 The Android Open Source Project
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 // http://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 #pragma once
16 
17 #include "aemu/base/memory/ScopedPtr.h"
18 #include "aemu/base/system/System.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include <locale.h>
23 
24 #define SKIP_TEST_ON_WINE()                        \
25     do {                                           \
26         if (System::get()->isRunningUnderWine()) { \
27             return;                                \
28         }                                          \
29     } while (0)
30 
31 inline auto setScopedCommaLocale() ->
32     android::base::ScopedCustomPtr<char, void(*)(const char*)> {
33 #ifdef _WIN32
34     static constexpr char commaLocaleName[] = "French";
35 #else
36     static constexpr char commaLocaleName[] = "fr_FR.UTF-8";
37 #endif
38 
39     // Set up a locale with comma as a decimal mark.
40     auto oldLocale = setlocale(LC_ALL, nullptr);
41     EXPECT_NE(nullptr, oldLocale);
42     if (!oldLocale) {
43         return {};
44     }
45     // setlocale() will overwrite the |oldLocale| pointer's data, so copy it.
46     auto oldLocaleCopy = android::base::ScopedCPtr<char>(strdup(oldLocale));
47     auto newLocale = setlocale(LC_ALL, commaLocaleName);
48     EXPECT_NE(nullptr, newLocale);
49     if (!newLocale) {
50         return {};
51     }
52 
53     EXPECT_STREQ(",", localeconv()->decimal_point);
54 
55     // Restore the locale after the test.
56     return android::base::makeCustomScopedPtr(
57             oldLocaleCopy.release(), [](const char* name) {
58                 auto nameDeleter = android::base::ScopedCPtr<const char>(name);
59                 EXPECT_NE(nullptr, setlocale(LC_ALL, name));
60             });
61 }
62