-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_write_test.c
52 lines (42 loc) · 1.25 KB
/
flash_write_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "flash_write_test.h"
#include <stdio.h>
#define FILE_SIZE 1024*1024
void flash_write_test()
{
printf("flash_write_test\n");
FILE *fp;
fp = fopen("write.file", "w");
// write pattern to file
for (int i = 0; i < FILE_SIZE; i++) {
fputc(i % 256, fp);
}
fclose(fp);
fp = fopen("write.file", "r");
// read in pattern and test for changes
int flips = 0;
int zeroToOneFlips = 0;
long size = FILE_SIZE;
for (int i = 0; i < FILE_SIZE; i++) {
int c = fgetc(fp);
if (c == EOF) {
size = ftell(fp);
break;
}
if (c != i % 256) {
/* now we count how many bits are different, and which way they flipped */
char xor = c ^ (i % 256);
for (int j = 0; j < 8; j++) {
/* check each bit of the xor */
if (xor & (1 << j)) {
flips++;
/* if the bit in c is 1, it means it has changed 0->1. */
if (c & (1 << j)) {
zeroToOneFlips++;
}
}
}
}
}
fclose(fp);
printf("total flips, %d\nzero to one flips, %d\nfile size, %ld\n", flips, zeroToOneFlips, size);
}