nulleric
/SpeedTestr
/amendments
/1
Initial
eric made amendment 1 over 2 years ago
--- main.c Wed Aug 10 15:31:13 2022
+++ main.c Wed Aug 10 15:31:13 2022
@@ -0,0 +1,158 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ShutDown.h>
+
+#define SIZE 1024
+
+// Prototypes
+unsigned int IntPow(unsigned int x, unsigned int times);
+short writeFile(unsigned int times);
+short readFile(unsigned int times);
+short verifyFile(void);
+
+// Buffer and pattern.
+char const gPattern = 85; //1010101
+char gBuff[SIZE];
+
+void main()
+{
+ unsigned int i = 1;
+ unsigned int p = 0;
+ short err = 0;
+
+ // Fill buffer with pattern
+ for(i = 0; i < SIZE ; i++)
+ {
+ gBuff[i] = gPattern;
+ }
+
+ for(i = 1; i < 12; i++)
+ {
+ p = IntPow(2, i);
+
+ printf("Writing: %d kb ", p);
+ err = writeFile(p);
+ if(err)
+ {
+ printf("Error writing file: %d", err);
+ return;
+ }
+
+ printf(" - Verifiying: %d kb ", p);
+ err = verifyFile();
+ if(err)
+ {
+ printf("Error verifying file: %d", err);
+ return;
+ }
+ printf(" - Reading: %d kb ", p);
+ err = readFile(p);
+ if(err)
+ {
+ printf("Error reading file: %d", err);
+ return;
+ }
+ }
+
+ printf("\nSuccess, Shutting down...");
+ //ShutDwnPower();
+}
+
+/* Blindly read the contents of the file n * SIZE */
+short readFile(unsigned int times)
+{
+ FILE * fp;
+ unsigned int i;
+ long beginTick = 0;
+ long endTick = 0;
+ int result;
+
+
+ fp = fopen("testfile.bin", "r");
+ if(fp == NULL)
+ return -2;
+ rewind(fp);
+ beginTick = TickCount();
+
+ for(i = 0; i != times; i++)
+ {
+ result = fread(gBuff, SIZE, 1, fp);
+ if(result != 1)
+ {
+ fclose(fp);
+ return -1;
+ }
+ }
+ endTick = TickCount();
+ fclose(fp);
+ printf(" took - %lu.%lu seconds\n", (endTick - beginTick) / 60, (endTick - beginTick) % 60);
+ return 0;
+}
+
+/* Verify the contents of the file match the pattern */
+short verifyFile()
+{
+ FILE * fp;
+ char c;
+
+ fp = fopen("testfile.bin", "r");
+ if(fp == NULL)
+ return -2;
+ rewind(fp);
+
+ while(true)
+ {
+ c = fgetc(fp);
+ if(c == EOF) break;
+ if(c != gPattern)
+ {
+ printf("VERIFY PATTERN ERR - %c", c);
+ fclose(fp);
+ return -1;
+ }
+ }
+ printf("Verified OK\n");
+ fclose(fp);
+ return 0;
+}
+
+/* Write a file with buffer n times */
+short writeFile(unsigned int times)
+{
+ FILE * fp;
+ unsigned int i;
+ int result;
+ long beginTick = 0;
+ long endTick = 0;
+
+ fp = fopen("testfile.bin", "w+");
+ if(fp == NULL)
+ return -2;
+ beginTick = TickCount();
+
+ for(i = 0; i != times; i++)
+ {
+ result = fwrite(gBuff, SIZE, 1, fp);
+ if(result != 1)
+ {
+ fclose(fp);
+ return -1;
+ }
+ }
+ endTick = TickCount();
+ fclose(fp);
+ printf(" took - %lu.%lu seconds\n", (endTick - beginTick) / 60, (endTick - beginTick) % 60);
+ return 0;
+}
+
+/* Calculate power for two ints */
+unsigned int IntPow(unsigned int x, unsigned int times)
+{
+ unsigned int result = 1;
+ unsigned int l = 0;
+ for(l = 1; l < times + 1; l++)
+ {
+ result = result * x;
+ }
+ return result;
+}