/* Copyright (c) 2005 by Devin Carraway
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "AutoMmap.h"

#include <sys/time.h>
#include <time.h>

#define TESTSIZE (1024 * 1024 * 16)

suseconds_t timesub(const struct timeval *t1, const struct timeval *t2)
{
	suseconds_t r;

	r = (t2->tv_sec - t1->tv_sec) * 1000000 +
	    (t2->tv_usec - t1->tv_usec);
	return r;
}

int main(int argc, char **argv)
{
    int i, c;
    int f;
    int *tmp;
    struct timeval tv1, tv2;

    f = open("t1", O_RDWR | O_CREAT | O_TRUNC, 0664);
    if (f < 0) {
        perror("t1");
        return 0;
    }
    tmp = (int *)malloc(sizeof(*tmp) * TESTSIZE);
    for (i = 0; i < TESTSIZE; i++)
        tmp[i] = i;
    write(f, tmp, sizeof(*tmp)*TESTSIZE);
    int *raw = (int*)mmap(NULL, sizeof(*tmp)*TESTSIZE, PROT_READ|PROT_WRITE,
		          MAP_SHARED, f, 0);
    if (raw == MAP_FAILED) {
	    perror("mmap");
	    return 2;
    }

    gettimeofday(&tv1, NULL);
    for (i = c = 0; i < TESTSIZE; i += 512, c++) {
	int r;
	lseek(f, sizeof(*raw) * i, SEEK_SET);
	read(f, &r, sizeof(r));
        if (r != i) {
            fprintf(stderr, "mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    gettimeofday(&tv2, NULL);
    printf("-1 ok : %d read() reads in %ld usec\n", c, timesub(&tv1,&tv2));

    // don't count this one
    for (i = 0; i < TESTSIZE; i += 512) {
        int r = *((int *)raw + i);
        if (r != i) {
            fprintf(stderr, "mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }

    gettimeofday(&tv1, NULL);
    for (i = c = 0; i < TESTSIZE; i += 512, c++) {
        int r = *((int *)raw + i);
        if (r != i) {
            fprintf(stderr, "+mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    for (i = TESTSIZE-512, c = 0; i > 0; i -= 512, c++) {
        int r = *((int *)raw + i);
        if (r != i) {
            fprintf(stderr, "-mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    gettimeofday(&tv2, NULL);
    printf("0 ok : %d raw reads in %ld usec\n", c, timesub(&tv1,&tv2));

    AutoMmap<int> m1(f, 1024);
    m1.mapcount();
    gettimeofday(&tv1, NULL);
    for (i = 0; i < TESTSIZE; i += 512) {
        int r = *(m1 + i);
        if (r != i) {
            fprintf(stderr, "+mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    for (c = 0, i = TESTSIZE-512; i > 0; i -= 512, c++) {
        int r = *(m1 + i);
        if (r != i) {
            fprintf(stderr, "-mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    gettimeofday(&tv2, NULL);
    printf("1 ok : %d * reads in %ld usec, %d remaps\n", c, timesub(&tv1,&tv2), m1.mapcount());

    gettimeofday(&tv1, NULL);
    for (i = 0; i < TESTSIZE; i += 512) {
        int r = m1[i];
        if (r != i) {
            fprintf(stderr, "+mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    for (c = 0, i = TESTSIZE-512; i > 0; i -= 512, c++) {
        int r = m1[i];
        if (r != i) {
            fprintf(stderr, "-mismatch at %d: expected %d, found %d\n",
                    i, i, r);
            return 1;
        }
    }
    gettimeofday(&tv2, NULL);
    printf("2 ok : %d [] reads in %ld usec, %d remaps\n", c, timesub(&tv1,&tv2), m1.mapcount());
    free(tmp);
    return 0;
}

