コード例 #1
0
ファイル: testlist.c プロジェクト: donliu/tommy_library
bool 
VerifyListConsistency(List *list)
{                                                       
    int i;    
    ListItor itor = ListHead(list);
    for (i = 0; i < length; i++) {                        
        if (ListValue(itor) != numbers[i]) {                   
            sprintf(error, "Encounter wrong data [i:%d/%d] when walking from the head to the tail", i, length-1); 
            return false;
        }                                               
        itor = ListItorNext(itor);
    }                                                   
    if (!ListItorNull(itor)) {                                     
        sprintf(error, "List has more elements at the end then numbers");                                
        return false;                                   
    }                                                   
    itor = ListTail(list);
    for (i = length-1; i >= 0; i--) {                      
        if (ListValue(itor) != numbers[i]) {                   
            sprintf(error, "Encounter wrong data [i:%d/%d] when walking from the tail to the head", i, length-1); 
            return false; 
        }
        itor = ListItorPrev(itor);
    }
    if (!ListItorNull(itor)) {
        sprintf(error, "List has more elements at the begining then numbers");                                
        return false; 
    } 
    return true; 
}
コード例 #2
0
ファイル: list.cpp プロジェクト: aelliott/GP2
List::List(label_t label)
    : QVector()
    , _clean(true)
{
    for(std::vector<atom_t>::iterator iter = label.values.begin();
        iter != label.values.end(); ++iter)
    {
        atom_t atom = *iter;
        std::string *str = boost::get<std::string>(&atom);
        int *intVal = boost::get<int>(&atom);

        if(str)
        {
            if(str->length() == 0)
                push_back(ListValue("empty"));
            else
            {
                if(str->at(0) == '"')
                    push_back(ListValue(Atom(str->c_str())));
                else
                    push_back(ListValue(str->c_str()));
            }
        }
        else if(intVal)
            push_back(ListValue(Atom(*intVal)));
        else
            qDebug() << "Variant containing no value passed to List()";
    }
}
コード例 #3
0
ファイル: main.c プロジェクト: murph141/CTCI
void checkPalindrome(List * list)
{
  if(ListValue(list) == ListValue2(list))
  {
    printf("The list is a palindrome\n");
  }
  else
  {
    printf("The list is not a palindrome\n");
  }
}
コード例 #4
0
ファイル: testlist.c プロジェクト: donliu/tommy_library
bool
TestListSearch(List *list)
{
    ListDeleteAll(list); // Already tested
    int i;
    for (i = 0; i < length; i++)
        ListAppend(list, numbers[i]);
    for (i = length - 1; i >= 0; i--)
        if (ListItorNull(ListSearch(list, numbers[i])) ||
                ListValue(ListSearch(list, numbers[i])) != numbers[i]) {
            sprintf(error, "Cannot find existing element [%d]", i);
            return false;
        }
    if (!ListItorNull(ListSearch(list, TestMaxValue + 1))) {
        sprintf(error, "Found nonexisting element - bigger than max");
        return false;
    }
    if (!ListItorNull(ListSearch(list, TestMinValue - 1))) {
        sprintf(error, "Found nonexisting element - smaller than min");
        return false;
    }
    return true;
}
コード例 #5
0
ファイル: testlist.c プロジェクト: donliu/tommy_library
bool
TestListDelete(List *list)
{
    /* Precondition */
    if (!VerifyListConsistency(list)){
        sprintf(error, "This test requires list to be consist with numbers!!");
        return false;
    }
    
    ListItor itor;

    /* Delete last element */
    if (!ListDelete(ListTail(list))) {
        sprintf(error, "Delete the first element failed");
        return false;
    }
    itor = ListTail(list);
    if (ListValue(itor) != numbers[length-2]
            || !ListItorNull(ListItorNext(itor))
            || ListValue(ListItorPrev(itor)) != numbers[length-3]) {
        sprintf(error, "Data is not consistent after deleting the first element");
        return false;
    }

    /* Delete the k'th element */
    int k = 5;
    int i;
    itor = ListHead(list);
    for (i = 0; i < k; i++) 
        itor = ListItorNext(itor);

    if (ListValue(itor) != numbers[k] 
            || ListValue(ListItorPrev(itor)) != numbers[k-1]
            || ListValue(ListItorNext(itor)) != numbers[k+1]) {
        sprintf(error, "Test code's assumption is wrong!!");
        return false;
    }

    if (!ListDelete(itor)) {
        sprintf(error, "ListDelete failed!!");
        return false;
    }
    itor = ListHead(list);
    for (i = 0; i < k; i++)
        itor = ListItorNext(itor);
    if (ListValue(itor) != numbers[k+1]
            || ListValue(ListItorPrev(itor)) != numbers[k-1]) {
        sprintf(error, "Data is not consistent after deleting the %dth element", k);
        return false;
    }

    /* Delete the first */
    if (!ListDelete(ListHead(list))) {
        sprintf(error, "Delete the first element failed");
        return false;
    }
    itor = ListHead(list);
    if (ListValue(itor) != numbers[1] 
            || !ListItorNull(ListItorPrev(itor))
            || ListValue(ListItorNext(itor)) != numbers[2]) {
        sprintf(error, "Data is not consistent after deleting the first element");
        return false;
    }

    /* Delete all the rest */
    for (i = 0; i < 10; i++)
        if (!ListDelete(ListItorPrev(ListTail(list)))) {
            sprintf(error, "Delete the last 2nd element failed when cleaning up");
            return false;
        }
    for (; i < 20; i++)
        if (!ListDelete(ListItorNext(ListHead(list)))) {
            sprintf(error, "Delete the 2nd element failed when cleaning up");
            return false;
        }
    for (; i < length - 40; i++)
        if (!ListDelete(ListTail(list))) {
            sprintf(error, "Delete the last element failed when cleaning up");
            return false;
        }
    for (; i < length - 3; i++)
        if (!ListDelete(ListHead(list))) {
            sprintf(error, "Delete the first element failed when cleaning up");
            return false;
        }

    if (!ListEmpty(list)) {
        sprintf(error, "List is not empty after delete all its elements");
        return false;
    }

    return true;
}
コード例 #6
0
ファイル: list.cpp プロジェクト: aelliott/GP2
List::List(const QString &labelStr)
    : QVector()
    , _clean(true)
{
    int labelPos = 0;
    bool needsValue = true;
    QRegExp rx;
    while(labelPos < labelStr.length())
    {
        // Check if we want a value or if we're expecting either an end of
        // string or a list value separator (:)
        if(!needsValue)
        {
            if(labelStr.at(labelPos) == QChar(':'))
            {
                needsValue = true;
                ++labelPos;
                continue;
            }
            else
            {
                qDebug() << "Unexpected char " << labelStr.at(labelPos) << " at "
                         << "position " << labelPos << ", was expecting a list "
                         << "value separator (:).";
                _clean = false;
                ++labelPos;
                continue;
            }
        }
        else if(labelStr.at(labelPos) == QChar(':'))
        {
            // If we encounter one right after another value then add an empty
            ++labelPos;
            push_back(ListValue("empty"));
            continue;
        }

        if(labelStr.at(labelPos) == QChar('\'')
                || labelStr.at(labelPos) == QChar('"'))
        {
            QChar matched = labelStr.at(labelPos);
            ++labelPos;
            int start = labelPos;
            while(labelPos < labelStr.length()
                  && labelStr.at(labelPos) != matched)
            {
                ++labelPos;
            }
            int len = labelPos - start;

            if(len == 0)
                push_back(ListValue(Atom("")));
            else
                push_back(ListValue(Atom(labelStr.mid(start, len))));

            if(labelPos < labelStr.length())
            {
                // We found a closing quote
                ++labelPos;
            }

            needsValue = false;
            continue;
        }

        rx = QRegExp("[a-zA-Z0-9_][a-zA-Z0-9_]{,62}");
        if(rx.indexIn(labelStr, labelPos) == labelPos)
        {
            QString identifier = rx.cap(0);
            int matchLength = rx.matchedLength();

            rx = QRegExp("\\d+");
            if(rx.indexIn(labelStr, labelPos) == labelPos &&
                    rx.matchedLength() >= matchLength)
            {
                labelPos += rx.matchedLength();
                QVariant num = rx.cap(0);
                int value = num.toInt();
                push_back(ListValue(Atom(value)));
                needsValue = false;
                continue;
            }
            labelPos += matchLength;
            push_back(ListValue(identifier));
            needsValue = false;
            continue;
        }

        qDebug() << "Unexpected char " << labelStr.at(labelPos) << " at  position "
                 << labelPos << ", was expecting an atom or variable.";
        _clean = false;
        ++labelPos;
    }
}