xref: /aosp_15_r20/external/google-breakpad/src/client/linux/minidump_writer/cpu_set_unittest.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2013 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>  // Must come first
31 #endif
32 
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
39 
40 #include "breakpad_googletest_includes.h"
41 #include "client/linux/minidump_writer/cpu_set.h"
42 #include "common/linux/scoped_tmpfile.h"
43 
44 using namespace google_breakpad;
45 
46 namespace {
47 
48 typedef testing::Test CpuSetTest;
49 
50 }
51 
TEST(CpuSetTest,EmptyCount)52 TEST(CpuSetTest, EmptyCount) {
53   CpuSet set;
54   ASSERT_EQ(0, set.GetCount());
55 }
56 
TEST(CpuSetTest,OneCpu)57 TEST(CpuSetTest, OneCpu) {
58   ScopedTmpFile file;
59   ASSERT_TRUE(file.InitString("10"));
60 
61   CpuSet set;
62   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
63   ASSERT_EQ(1, set.GetCount());
64 }
65 
TEST(CpuSetTest,OneCpuTerminated)66 TEST(CpuSetTest, OneCpuTerminated) {
67   ScopedTmpFile file;
68   ASSERT_TRUE(file.InitString("10\n"));
69 
70   CpuSet set;
71   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
72   ASSERT_EQ(1, set.GetCount());
73 }
74 
TEST(CpuSetTest,TwoCpusWithComma)75 TEST(CpuSetTest, TwoCpusWithComma) {
76   ScopedTmpFile file;
77   ASSERT_TRUE(file.InitString("1,10"));
78 
79   CpuSet set;
80   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
81   ASSERT_EQ(2, set.GetCount());
82 }
83 
TEST(CpuSetTest,TwoCpusWithRange)84 TEST(CpuSetTest, TwoCpusWithRange) {
85   ScopedTmpFile file;
86   ASSERT_TRUE(file.InitString("1-2"));
87 
88   CpuSet set;
89   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
90   ASSERT_EQ(2, set.GetCount());
91 }
92 
TEST(CpuSetTest,TenCpusWithRange)93 TEST(CpuSetTest, TenCpusWithRange) {
94   ScopedTmpFile file;
95   ASSERT_TRUE(file.InitString("9-18"));
96 
97   CpuSet set;
98   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
99   ASSERT_EQ(10, set.GetCount());
100 }
101 
TEST(CpuSetTest,MultiItems)102 TEST(CpuSetTest, MultiItems) {
103   ScopedTmpFile file;
104   ASSERT_TRUE(file.InitString("0, 2-4, 128"));
105 
106   CpuSet set;
107   ASSERT_TRUE(set.ParseSysFile(file.GetFd()));
108   ASSERT_EQ(5, set.GetCount());
109 }
110 
TEST(CpuSetTest,IntersectWith)111 TEST(CpuSetTest, IntersectWith) {
112   ScopedTmpFile file1;
113   ASSERT_TRUE(file1.InitString("9-19"));
114 
115   CpuSet set1;
116   ASSERT_TRUE(set1.ParseSysFile(file1.GetFd()));
117   ASSERT_EQ(11, set1.GetCount());
118 
119   ScopedTmpFile file2;
120   ASSERT_TRUE(file2.InitString("16-24"));
121 
122   CpuSet set2;
123   ASSERT_TRUE(set2.ParseSysFile(file2.GetFd()));
124   ASSERT_EQ(9, set2.GetCount());
125 
126   set1.IntersectWith(set2);
127   ASSERT_EQ(4, set1.GetCount());
128   ASSERT_EQ(9, set2.GetCount());
129 }
130 
TEST(CpuSetTest,SelfIntersection)131 TEST(CpuSetTest, SelfIntersection) {
132   ScopedTmpFile file1;
133   ASSERT_TRUE(file1.InitString("9-19"));
134 
135   CpuSet set1;
136   ASSERT_TRUE(set1.ParseSysFile(file1.GetFd()));
137   ASSERT_EQ(11, set1.GetCount());
138 
139   set1.IntersectWith(set1);
140   ASSERT_EQ(11, set1.GetCount());
141 }
142 
TEST(CpuSetTest,EmptyIntersection)143 TEST(CpuSetTest, EmptyIntersection) {
144   ScopedTmpFile file1;
145   ASSERT_TRUE(file1.InitString("0-19"));
146 
147   CpuSet set1;
148   ASSERT_TRUE(set1.ParseSysFile(file1.GetFd()));
149   ASSERT_EQ(20, set1.GetCount());
150 
151   ScopedTmpFile file2;
152   ASSERT_TRUE(file2.InitString("20-39"));
153 
154   CpuSet set2;
155   ASSERT_TRUE(set2.ParseSysFile(file2.GetFd()));
156   ASSERT_EQ(20, set2.GetCount());
157 
158   set1.IntersectWith(set2);
159   ASSERT_EQ(0, set1.GetCount());
160 
161   ASSERT_EQ(20, set2.GetCount());
162 }
163 
164