Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created May 2, 2014 22:05
Show Gist options
  • Save TimSC/7acae6a392fd1dbdf2ba to your computer and use it in GitHub Desktop.
Save TimSC/7acae6a392fd1dbdf2ba to your computer and use it in GitHub Desktop.
This program does test reads and writes to a file.
//Reading and writing tests
//Release under CC0 by Tim Sheerman-Chase, 2014
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
FILE *fi = fopen("soaktest-cpp.dat", "w+b");
int globalRandSeed = 0;
srand(globalRandSeed);
int numBins = 1000;
cout << "Generate bin sizes" << endl;
vector<unsigned> binSizes;
int total = 0;
for(unsigned i=0;i<numBins;i++)
{
int s = rand()%500001;
binSizes.push_back(s);
total += s;
}
cout << "Total test size " << total << endl;
vector<unsigned> binOffsets;
binOffsets.push_back(0);
for(int i=0; i< binSizes.size()-1;i++)
{
unsigned val = binOffsets[binOffsets.size()-1]+binSizes[i];
binOffsets.push_back(val);
}
vector<string> oldData;
int oldSeed;
while(1)
{
cout << "Begin iteration "<< globalRandSeed<< endl;
globalRandSeed += 1;
srand(globalRandSeed);
//Generate new data
vector<string> data;
for(int i=0; i < binSizes.size();i++)
{
string tmp;
for(int j=0;j<binSizes[i];j++)
{
tmp.append(1, (char)(rand()%256));
}
data.push_back(tmp);
//cout << tmp.size() << "," << binSizes[i] << endl;
}
//Generate write order
list<unsigned> accessOrder;
for(int i=0; i < binSizes.size();i++)
{
accessOrder.push_back(i);
}
vector<unsigned> accessOrderShuffled;
while(accessOrder.size())
{
unsigned ind = rand() % accessOrder.size();
list<unsigned>::iterator it = accessOrder.begin();
for(int i=0;i<ind;i++)
it ++;
accessOrderShuffled.push_back(*it);
accessOrder.erase(it);
}
for(unsigned int i=0; i < accessOrderShuffled.size(); i++)
{
unsigned binNum = accessOrderShuffled[i];
if(oldData.size()>0)
{
fseek(fi, binOffsets[binNum], SEEK_SET);
char buff[500000];
int readsize = fread(buff, 1, binSizes[binNum], fi);
int checkok = 1;
if(readsize!=binSizes[binNum])
checkok = 0;
int countMatch = 0;
for(int j=0;j< readsize;j++)
if(oldData[binNum].c_str()[j] == buff[j])
countMatch += 1;
if(countMatch != binSizes[binNum])
checkok = 0;
if (!checkok)
{
cout << "Readback failed." << endl;
cout << binSizes[binNum] << "," << readsize << "," << countMatch << endl;
cout << "expected:";
for (int j=0;j<30;j++)
cout << ((int)(oldData[binNum].c_str()[j])) << ",";
cout << endl;
cout << "got :";
for (int j=0;j<30;j++)
cout << ((int)(buff[j])) << ",";
cout << endl;
exit(0);
}
}
fseek(fi, binOffsets[binNum], SEEK_SET);
fwrite(data[binNum].c_str(), 1, data[binNum].size(), fi);
}
//Flush every tenth interation to see if it makes a difference
if(globalRandSeed % 10 == 0)
{
cout << "Flushing" << endl;
fflush(fi);
}
oldData = data;
oldSeed = globalRandSeed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment