我是一名CS学生,我参加本学期的其中一门课程要求我完成不同的作业(用C ++编写),然后用我们教授编写的单元测试来测试它们。
Link to the repository with tests and the skeleton of the exercises
la1的所有测试都运行得很好。可悲的是,当我开始测试lab2练习中的代码时,大多数人都停止了工作。所有测试都将相同的消息打印到控制台:
从gtest_main.cc运行main()
处理以退出代码139结束(由信号11:SIGSEGV中断)
今天,在我完成了lab2->最佳产品运动
之后给定一个整数数组,写一个函数maxProductFinderK,它可以从数组中的任何k个整数中获得。
我手工检查了所有的测试结果,我得到的结果都是正确的,所以我不认为我的代码是真正的问题。
有什么我可以仔细检查来解决这个问题吗?到目前为止,我已经尝试过:
再次使用练习克隆存储库
使用不同的解决方案只是为了确保代码本身不是问题
我在Mac OS 10.13.3上运行CLion中的所有测试(使用最新更新),如果相关的话。
//编辑:按照要求我在这里添加了代码,以防链接停止工作。
作业解决方案:
int GreatestProduct(const std::vector<int> &numbers, int k) {
if ( numbers.size() > 0) {
std::vector<int> largestK = numbers;
std::sort(largestK.begin(), largestK.end());
if (k == 0) {
return 0;
} else if (k == 1) {
return largestK.at(largestK.size() - 1);
}
largestK.erase(largestK.begin(), largestK.end() - (k)); // remove all but k largest elements
std::vector<int> smallestK = numbers;
std::sort(smallestK.begin(), smallestK.end());
smallestK.erase(smallestK.begin(),
smallestK.begin() + k - (k % 2)); // remove all but k-(k%2) smallest elements
int mustContain = (k % 2) * (largestK.at(0) - 1) + 1;
//make sure that largestK and smallestK are the same size
if (k % 2 != 0) {
largestK.erase(largestK.begin());
}
std::vector<int> final;
int currentLargest = 0;
int currentSmallest = 0;
int greatestProduct = 1;
while (currentLargest + currentSmallest < k - (k % 2)) {
int firstOption = largestK.at(currentLargest) * largestK.at(currentLargest + 1);
int secondOption = smallestK.at(currentSmallest) * smallestK.at(currentSmallest + 1);
if (mustContain * firstOption > mustContain * secondOption) {
final.push_back(firstOption);
currentLargest += 2;
} else {
final.push_back(secondOption);
currentSmallest += 2;
}
}
for (int element : final) {
greatestProduct *= element;
}
return greatestProduct;
} else {
return 0;
} }
首先进行3次测试。这是最简单的,因为它总是假设k = 2并且它不测试负整数。
#include <GreatestProduct.h>
#include <gtest/gtest.h>
#include <MemLeakTest.h>
#include <StringUtility.h>
using TestParam = std::pair<std::pair<std::vector<int>, int>, int>;
class GreatestProductOfStep1Tests : public ::testing::TestWithParam<TestParam>, MemLeakTest {
};
TEST_P(GreatestProductOfStep1Tests, GreatestProductOfPositiveNumbersNfixedTo2ShouldReturnExpectedResult) {
const TestParam &p = GetParam();
int expected = p.second;
const std::vector<int> &numbers = p.first.first;
int N = p.first.second;
EXPECT_EQ(expected, GreatestProduct(numbers, N))
<< "Did call GreatestProductOf(" << utility::ToString<int>(numbers) << ", " << N << ")\n";
}
std::vector<TestParam> greatest_product_test_positive_n_eq_2_data{
{{{0, 1, 2, 3, 4}, 2}, 12},
{{{6, 6, 6}, 2}, 36},
{{{9, 8, 3, 5, 8, 1, 3, 5, 10}, 2}, 90},
{{{17, 5, 9, 1000, 15689, 57, 89, 10, 89, 283, 197, 0, 0, 132, 45, 78, 18, 15, 89,
19203, 98, 14, 78, 45, 35, 23, 24, 25, 46, 45, 756, 7567, 123, 890, 99, 98, 51,
991, 9123, 8912, 89534, 8923, 1823, 7385, 91, 1748, 1, 1, 893, 2813,
1381, 23, 563, 645, 24, 24, 51, 839, 38, 34, 35, 123, 324, 9283, 22, 19}, 2}, 1719321402},
{{{1, 1}, 2}, 1},
{{{0, 1}, 2}, 0},
{{{3789, 999}, 2}, 3785211}};
INSTANTIATE_TEST_CASE_P(GreatestProductOfStep1Tests,
GreatestProductOfStep1Tests,
::testing::ValuesIn(greatest_product_test_positive_n_eq_2_data));