blob: bfba452acf3630806379c1f39a01c56868af0c59 [file] [log] [blame]
/*
* Copyright (C) 2014 The Android Open source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <search.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
struct FooStruct {
int x;
int y;
int z;
} FOOs[10];
size_t FOOCnt = 0;
const FOOSIZE = sizeof(struct FooStruct);
int match(const void *k, const void *v) {
const struct FooStruct* key = (const struct FooStruct*) k;
const struct FooStruct* val = (const struct FooStruct*) v;
if ((NULL == key) || (NULL == val)) {
printf("Bogus value passed to match.\n");
exit(1);
};
if (key->x == val->x && key->y == val->y && key->z == val->z)
return 0;
return 1;
}
int InsertAtEnd(struct FooStruct* f) {
size_t old_cnt = FOOCnt;
if (NULL != lfind(f, FOOs, &FOOCnt, FOOSIZE, match)) {
printf("Unexpectedly found FOO.\n");
return 1;
}
struct FooStruct *ptr = lsearch(f, FOOs, &FOOCnt, FOOSIZE, match);
if (ptr != &FOOs[old_cnt]) {
printf("Expectedly to add at end.\n");
return 1;
}
if (FOOCnt != (old_cnt + 1)) {
printf("Expected to grow cnt %d vs %d.\n", (int) FOOCnt, (int) old_cnt);
return 1;
}
if (lfind(f, FOOs, &FOOCnt, FOOSIZE, match) != ptr) {
printf("Expected to find foo.\n");
return 1;
}
return 0;
}
int main(int argc, char* argv[]) {
struct FooStruct FooA = { 1, 2, 3 };
struct FooStruct FooB = { 2, 3, 4 };
int cnt = 0;
cnt += InsertAtEnd(&FooA);
cnt += InsertAtEnd(&FooB);
return cnt;
}