Beispiel #1
0
void test_search_element_from_the_String_array(){
    String elements[3] = {"aaa","bbb","ccc"};
    String key = "aaa";
    String* result = search_generic(elements,&key, 3, sizeof(String), compareString);
    ASSERT(&elements[0] == result);
};
Beispiel #2
0
void test_search_element_from_the_String_array_which_is_not_found(){
    String elements[3] = {"aaa","bbb","ccc"};
    String key = "kkk";
    String* result = search_generic(elements,&key, 3, sizeof(String), compareString);
    ASSERT(NULL == (String*)result);
};
Beispiel #3
0
void test_search_element_from_the_float_array_which_is_not_found(){
    float elements[5] = {4.7f,6.4f,7.3f,8.5f,2.2f};
    float key = 4.6f;
    float* result = search_generic(elements,&key, 5, sizeof(float), compareInt);
    ASSERT(NULL == (float*)result);
};
Beispiel #4
0
void test_search_element_from_the_double_array(){
    double elements[5] = {1.0,2.0,3.0,4.0,5.0};
    double key = 3.0;
    double* result = search_generic(elements,&key, 5, sizeof(double), compareDouble);
    ASSERT(&elements[2] == result);
};
Beispiel #5
0
void test_search_last_element_from_the_integer_array(){
    int elements[] = {1,2,3,4,5};
    int key = 5;
    int* result = search_generic(elements,&key, 5, sizeof(int), compareInt);
    ASSERT(&elements[4] == result);
};
Beispiel #6
0
void test_search_element_from_the_float_array(){
    float elements[5] = {1.5f,2.5f,3.5f,4.5f,5.5f};
    float key = 2.5f;
    float* result = search_generic(elements,&key, 5, sizeof(float), compareInt);
    ASSERT(&elements[1] == result);
};
Beispiel #7
0
void test_search_1st_element_from_the_integer_array(){
    int elements[5] = {4,9,3};
    int key = 4;
    int* result = search_generic(elements,&key, 2, sizeof(int), compareInt);
    ASSERT(&elements[0] == result);
};
Beispiel #8
0
void test_search_middle_element_from_the_integer_array(){
    int elements[5] = {5,5,7,8,2};
    int key = 7;
    int* result = search_generic(elements,&key, 5, sizeof(int), compareInt);
    ASSERT(&elements[2] == result);
};
Beispiel #9
0
void test_search_element_from_the_integer_array(){
    int elements[5] = {3,6,0,8,1};
    int key = 8;
    int* result = search_generic(elements,&key, 5, sizeof(int), compareInt);
    ASSERT(&elements[3] == result);
};
Beispiel #10
0
void test_search_element_from_the_integer_array_which_not_found(){
    int elements[5] = {1,2,3,4,5};
    int key = 9;
    int* result = search_generic(elements,&key, 5, sizeof(int), compareInt);
    ASSERT((int*)result == NULL);
};
Beispiel #11
0
void test_search_last_element_from_the_character_array_if_elements_are_same_with_diff_characters(){
    char elements[3] = {'a','a','b'};
    char key = 'a';
    char* result = search_generic(elements,&key, 3, sizeof(char), compareChar);
    ASSERT(&elements[1] == result);
};
Beispiel #12
0
void test_search_first_element_from_thecharacter_array_if_elements_are_same(){
    char elements[2] = {'a','a'};
    char key = 'a';
    char* result = search_generic(elements,&key, 2, sizeof(char), compareChar);
    ASSERT(&elements[0] == result);
};
Beispiel #13
0
void test_search_element_from_the_character_array_which_is_not_found(){
    char elements[3] = {'a','b','c'};
    char key = 'k';
    char* result = search_generic(elements,&key, 3, sizeof(char), compareChar);
    ASSERT(NULL == (char*)result);
};
Beispiel #14
0
void test_search_element_from_the_character_array(){
    char elements[3] = {'a','b','c'};
    char key = 'b';
    char* result = search_generic(elements,&key, 3, sizeof(char), compareChar);
    ASSERT(&elements[1] == result);
};
Type*
Namespace::Search(const string& name)
{
    // an exact match wins
    Type* result = Find(name);
    if (result != NULL) {
        return result;
    }

    // try the class names
    // our language doesn't allow you to not specify outer classes
    // when referencing an inner class.  that could be changed, and this
    // would be the place to do it, but I don't think the complexity in
    // scoping rules is worth it.
    int N = m_types.size();
    for (int i=0; i<N; i++) {
        if (m_types[i]->Name() == name) {
            return m_types[i];
        }
    }

    // we got to here and it's not a generic, give up
    if (name.find('<') == name.npos) {
        return NULL;
    }

    // remove any whitespace
    string normalized = normalize_generic(name);

    // find the part before the '<', find a generic for it
    ssize_t baseIndex = normalized.find('<');
    string base(normalized.c_str(), baseIndex);
    const Generic* g = search_generic(base);
    if (g == NULL) {
        return NULL;
    }

    // For each of the args, do a recursive search on it.  We don't allow
    // generics within generics like Java does, because we're really limiting
    // them to just built-in container classes, at least for now.  Our syntax
    // ensures this right now as well.
    vector<Type*> args;
    size_t start = baseIndex + 1;
    size_t end = start;
    while (normalized[start] != '\0') {
        end = normalized.find(',', start);
        if (end == normalized.npos) {
            end = normalized.find('>', start);
        }
        string s(normalized.c_str()+start, end-start);
        Type* t = this->Search(s);
        if (t == NULL) {
            // maybe we should print a warning here?
            return NULL;
        }
        args.push_back(t);
        start = end+1;
    }

    // construct a GenericType, add it to our name set so they always get
    // the same object, and return it.
    result = make_generic_type(g->package, g->name, args);
    if (result == NULL) {
        return NULL;
    }

    this->Add(result);
    return this->Find(result->QualifiedName());
}