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