您的位置:首页 > 其它

"《算法导论》之‘线性表’":基于静态分配的数组的顺序表

2014-10-18 18:46 573 查看
  首先,我们来搞明白几个概念吧(参考自网站数据结构及百度百科)。

  线性表

  线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。在实现线性表数据元素的存储方面,一般可用顺序存储结构和链式存储结构两种方法。

  顺序表

  用顺序存储方法存储的线性表简称为顺序表(Sequential List)。顺序表的存储方法是把线性表的结点按逻辑次序依次存放在一组地址连续的存储单元里。

  链表

  链接方式存储的线性表简称为链表(Linked List)。

  顺序表和链表的比较如下:

  

// BoostUnitTest.cpp
#define  BOOST_TEST_MODULE ArrayList_Test_Module

#include "stdafx.h"
#include "D:\VSProject\Algorithm\List\SeqList\SeqList\SeqList\seqlist.h"

struct ArrayList_Fixture
{
ArrayList_Fixture()
{
BOOST_TEST_MESSAGE("Setup fixture");
testArrayList = new SeqList();
}
~ArrayList_Fixture()
{
BOOST_TEST_MESSAGE("Teardown fixture");
delete testArrayList;
}

SeqList * testArrayList;
};

// BOOST_AUTO_TEST_SUITE(ArrayList_Test_Suite)
BOOST_FIXTURE_TEST_SUITE(ArrayList_Test_Suite, ArrayList_Fixture)

BOOST_AUTO_TEST_CASE(ArrayList_Abnormal_Test)
{
// Set values to the array list
int testArray[] = { 1, 2, 3, 4, 5 };    // 5 个元素
int testLenOfList = sizeof(testArray) / sizeof(int);
testArrayList->setSeqList(testArray, testLenOfList);
// BOOST_REQUIRE_THROW(testArrayList->setArrayList(testArray, testLenOfList), out_of_range);

// Method getItem-----------------------------------------------
// If the position of the item you want to get is less than zero
BOOST_REQUIRE_THROW(testArrayList->getItem(-1), out_of_range);
// If the position of the item you want to get is larger than the length of the list
BOOST_REQUIRE_THROW(testArrayList->getItem(10), out_of_range);

// Method insert-------------------------------------------------
// If the inserting position is less than zero
BOOST_REQUIRE_THROW(testArrayList->insert(-1, 10), out_of_range);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList);

// If the inserting position is larger than the length of the list
BOOST_REQUIRE_THROW(testArrayList->insert(10, 10), out_of_range);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList);

// Method erase-------------------------------------------------
// If the erasing position is less than zero
BOOST_REQUIRE_THROW(testArrayList->erase(-1), out_of_range);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList);

// If the erasing position is larger than the length of the list
BOOST_REQUIRE_THROW(testArrayList->erase(10), out_of_range);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList);

}

BOOST_AUTO_TEST_CASE(ArrayList_Normal_Test)
{
bool expected;
bool actual;
// Method empty-------------------------------------------------
expected = true;
actual = testArrayList->empty();
BOOST_REQUIRE(expected == actual);

// Set values to the array list
int testArray[] = { 1, 2, 3, 4, 5 };    // 5 个元素
int testLenOfList = sizeof(testArray) / sizeof(int);
testArrayList->setSeqList(testArray, testLenOfList);
// BOOST_REQUIRE_THROW(testArrayList->setArrayList(testArray, testLenOfList), out_of_range);

// Method getItem-----------------------------------------------
BOOST_REQUIRE(testArrayList->getItem(1) == testArray[1]);

// Method empty-------------------------------------------------
expected = false;
actual = testArrayList->empty();
BOOST_REQUIRE(expected == actual);

// Method insert-------------------------------------------------
expected = true;
actual = testArrayList->insert(1, 10);
BOOST_REQUIRE(expected == actual);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList + 1);
BOOST_REQUIRE(testArrayList->getItem(1) == 10);

// Method erase-------------------------------------------------
expected = true;
actual = testArrayList->erase(1);
BOOST_REQUIRE(expected, actual);
BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList);
BOOST_REQUIRE(testArrayList->getItem(1) == testArray[1]);

}

BOOST_AUTO_TEST_SUITE_END();


BoostUnitTest.cpp
  我在单元测试中偏向于使用REQUIRE型的判断,这样方便我检测出问题出现在哪。另外,我在单元测试中偏向于按“正常测试”和“不正常测试”两类进行测试。

  本篇博文的代码均托管到Taocode : http://code.taobao.org/p/datastructureandalgorithm/src/.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: