简体   繁体   English

CPP c弦切片和单元测试

[英]CPP c-string Slice and Unit Test

The point of this project is to build your own unit test and use c-strings. 该项目的重点是构建自己的单元测试并使用c字符串。

The project: DNA Sequencing Strand 项目:DNA测序链

Here is an example of two DNA strands that match when overlapped: 这是重叠时匹配的两条DNA链的示例:

Strand 1: ACGGACATAGTCATT
Strand 2:      CATAGTCATTTCATG
Combined: ACGGACATAGTCATTTCATG

I am at a loss when I try to implement the following: 当我尝试实现以下内容时,我很茫然:

Strand must have a method Strand substrand(size_t i, size_t j) const that returns a copy of the strand that starts at position i and ends at position j - 1 . Strand必须具有方法Strand substrand(size_t i, size_t j) const ,该方法返回从位置i开始到位置j - 1结束的链的副本。 Think “Python slice.” 考虑一下“ Python slice”。

update: I have recently answered my own question and updated the void test_substrand() and Strand Strand::substrand(size_t start, size_t end) const . 更新:我最近回答了我自己的问题,并更新了void test_substrand()Strand Strand::substrand(size_t start, size_t end) const I am getting a segmentation fault. 我遇到了细分错误。

Here is my code so far with filename underneath its body. 到目前为止,这是我的代码,其代码位于其主体下方。

void test_substrand()
{
    Strand s1;
    s1.setStrand("Hellow World");
    s1.substrand(1, 4);
    std::cout << "Substrand: " << s1.getStrand() << std::endl;
}

int main()
{
    test_substrand();
    return 0;
}

test.cpp 测试文件

#ifndef _STRAND_H_
#define _STRAND_H_
#include <cstdlib>
#include <string>
#include <string>

class Strand
{
public:
  Strand();
  Strand(const char *src);
  ~Strand();
  Strand & operator=(const Strand &rhs);
  const char *getStrand() const;
  void setStrand(const char *strand);
  Strand & operator+=(const Strand &rhs);
  Strand operator+(const Strand &rhs) const;

  Strand substrand(size_t start, size_t end) const;

protected:
  char *mStrand;
};

#endif /* _STRAND_H_ */
/* Local Variables: */
/* mode:c++         */
/* End:             */

Strand.h

#include "Strand.h"
#include <cstring>
#include <string>

// default constructor
Strand::Strand()
  : mStrand(0)
{
}

// copy constructor
Strand::Strand(const char *src)
  : mStrand(0)
{
  *this = src;
}

// destructor
Strand::~Strand()
{
  if(mStrand != 0)
  {
    delete [] mStrand;
    mStrand = 0;
  }
}

// assignment operator
Strand &Strand::operator=(const Strand &rhs)
{
  setStrand(rhs.mStrand);
  return *this;
}

// Get Strand
const char *Strand::getStrand() const
{
  return mStrand;
}

// Set Strand
void Strand::setStrand(const char *strand)
{
  if(mStrand)
  {
    delete [] mStrand;
    mStrand = 0;
  }
  if(strand)
  {
    mStrand = new char [std::strlen(strand) + 1];
    std::strcpy(mStrand, strand);                 
  }
}

size_t Strand::size() const
{
  return mBases;
}

// operator +=
Strand &Strand::operator+=(const Strand &rhs)
{
  char *new_strand;
  new_strand = new char [std::strlen(mStrand) + std::strlen(rhs.mStrand) + 1];

  strcat(strcpy(new_strand, mStrand), rhs.mStrand); 

  setStrand(new_strand);
  delete [] new_strand;
  return *this;
}

// operator +
Strand Strand::operator+(const Strand &rhs) const
{
  Strand value;
  value = *this;
  value += rhs;
  return value;
}

// substrand "python" slice
Strand Strand::substrand(size_t start, size_t end) const
{
  size_t k, i, size;

  size = end - start;

  char *new_strand;
  new_strand = new char [size +1];
  for (k = start, i = 0; k < end; k++, i++)
  {
    new_strand[i] = mStrand[k];
  }

  new_strand[k] = 0;
  Strand setStrand(new_strand);
  delete [] new_strand;
  new_strand = 0;
  return setStrand;
}

Strand.cpp

There are two issues in the fixed code. 固定代码中有两个问题。

First, the testing functions ignores the result of substrand and always prints the original strand. 首先,测试功能会忽略子链的结果,并始终打印原始链。 This is the fixed code: 这是固定的代码:

void test_substrand()
{
    Strand s1;
    s1.setStrand("Hellow World");
    Strand s2 = s1.substrand(1, 4);
    std::cout << "Substrand: " << s2.getStrand() << std::endl;
}

Second, your copy constructor had: 其次,您的副本构造函数具有:

  *this = src;

It caused unlimited recursive calls to this copy constructor and the application crashed on stack exhaustion. 它导致对该副本构造函数的无限递归调用,并且应用程序在堆栈耗尽时崩溃。 This is a fix 这是一个修复

Strand::Strand(const char *src)
  : mStrand(0)
{
   mStrand = new char [std::strlen(src)+ 1];
   std::strcpy(mStrand, src);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM