42 lines
1.4 KiB
C
42 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "numbers.h"
|
|
|
|
int main(void)
|
|
{
|
|
unsigned int len = 100;
|
|
unsigned int *nums = createNumbers(len);
|
|
if(nums == NULL) { fprintf(stderr, "createNumbers returned NULL\n"); return 1; }
|
|
|
|
// count occurrences
|
|
unsigned int maxVal = 2 * len;
|
|
unsigned int *counts = calloc(maxVal + 1, sizeof(unsigned int));
|
|
if(counts == NULL) { free(nums); return 2; }
|
|
|
|
for(unsigned int i = 0; i < len; i++)
|
|
{
|
|
if(nums[i] > maxVal) { fprintf(stderr, "value out of expected range\n"); free(nums); free(counts); return 3; }
|
|
counts[nums[i]]++;
|
|
}
|
|
|
|
int duplicatesFound = 0;
|
|
unsigned int duplicateValue = 0;
|
|
for(unsigned int v = 1; v <= maxVal; v++)
|
|
{
|
|
if(counts[v] == 2) { duplicatesFound++; duplicateValue = v; }
|
|
else if(counts[v] > 2) { fprintf(stderr, "value %u appears more than twice\n", v); free(nums); free(counts); return 4; }
|
|
}
|
|
|
|
if(duplicatesFound != 1) { fprintf(stderr, "expected exactly one duplicated value, found %d\n", duplicatesFound); free(nums); free(counts); return 5; }
|
|
|
|
unsigned int found = getDuplicate(nums, len);
|
|
if(found != duplicateValue) { fprintf(stderr, "getDuplicate returned %u expected %u\n", found, duplicateValue); free(nums); free(counts); return 6; }
|
|
|
|
free(nums);
|
|
free(counts);
|
|
|
|
printf("test_numbers: OK (duplicate = %u)\n", duplicateValue);
|
|
return 0;
|
|
}
|