Skip to contentSkip to conversation

elevator with layers

DISCUSSION Rick Hohensee 1 participant 1 message Jul 18, 2001

lore
Share & tools
lore
#0 lore raw

A guy posted some test results based on some code like this...

#include <stdio.h>
#include <assert.h>

#define TEST_SZ 25000000
#define RD_BUFF_SZ 5000
int main(int argc, const char **argv, const char **env)

{
    FILE* fp;

    if(argc > 1) fp = fopen(argv[1], "r+");
    else fp =tmpfile();
    if(NULL != fp) {
        int j = -1;
        int o;
        while(1) {
            if(++j != TEST_SZ) {
                if (j == (TEST_SZ - RD_BUFF_SZ) ) o = ftello(fp);
                fwrite(&j, sizeof(int), 1, fp);
            } else {
                int i, buffer[RD_BUFF_SZ];
                fflush(fp);
                fseek(fp, o, SEEK_SET);
                fread(buffer, sizeof(int), sizeof(buffer), fp);
                printf("Validating end of file writes\n");
                for(i = (RD_BUFF_SZ - 1); i >= 0; i--) {
                    assert(buffer[i] == --j) ;
                }
                rewind(fp);
                j = -1;
            }
        }
        return 1;
    }
    return 0;
}

That's not it exactly. o wasn't an int. Probably a long long by some other name. I don't know what he's trying to do with the above, but I believe the following has the same basic action...

#include <stdio.h>
#include <assert.h>

int main () {

int i, j, buffer[5000], isize, bufsize;

isize = sizeof(int);
bufsize = sizeof(buffer);

FILE * stream = tmpfile();

top:    j = -1;
        fflush(stream);
        fseek(stream, 0, SEEK_SET);
        fread(buffer, isize, bufsize, stream);
        printf("x            ");
        for(i = 4999; i >=0 ; i --) { assert(buffer[i] == --j) ; }
        rewind(stream);

goto top ;
}

which should be a bit easier to assess if I'm right about what it's doing. There's probably still some stuff there that can go away, but I'm not familiar with rewind() and friends.

Rick Hohensee
www.clienux.com