Skip to content

Commit 44968ec

Browse files
upy-fs-builder: Add tests for calculating a file size in the fs.
1 parent 7ecafca commit 44968ec

File tree

1 file changed

+162
-133
lines changed

1 file changed

+162
-133
lines changed

src/__tests__/micropython-fs-builder.spec.ts

Lines changed: 162 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { strToBytes } from '../common';
66
import {
77
addIntelHexFile,
88
addIntelHexFiles,
9+
calculateFileSize,
910
getIntelHexFiles,
1011
getIntelHexFsSize,
1112
testResetFileSystem,
@@ -16,142 +17,142 @@ const makecodeHexFile = fs.readFileSync('./src/__tests__/makecode.hex', 'utf8');
1617
const randContent = strToBytes('Some random content.');
1718

1819
describe('Writing files to the filesystem.', () => {
19-
it('Add files to hex.', () => {
20-
const files = [
21-
{
22-
fileName: 'test_file_1.py',
23-
fileStr: "from microbit import display\r\ndisplay.show('x')",
24-
},
25-
{
26-
fileName: 'test_file_2.py',
27-
fileStr:
28-
'# Lorem Ipsum is simply dummy text of the printing and\r\n' +
29-
"# typesetting industry. Lorem Ipsum has been the industry's\r\n" +
30-
'# standard dummy text ever since the 1500s, when an unknown\r\n' +
31-
'# printer took a galley of type and scrambled it to make a\r\n' +
32-
'# type specimen book. It has survived not only five\r\n' +
33-
'# centuries, but also the leap into electronic typesetting,\r\n' +
34-
'# remaining essentially unchanged. It was popularised in the\r\n' +
35-
'# 1960s with the release of Letraset sheets containing Lorem\r\n' +
36-
'# Ipsum passages, and more recently with desktop publishing\r\n' +
37-
'# software like Aldus PageMaker including versions of Lorem\r\n' +
38-
'# Ipsum.\r\n' +
39-
'# Lorem Ipsum is simply dummy text of the printing and\r\n' +
40-
"# typesetting industry. Lorem Ipsum has been the industry's\r\n" +
41-
'# standard dummy text ever since the 1500s, when an unknown\r\n' +
42-
'# printer took a galley of type and scrambled it to make a\r\n' +
43-
'# type specimen book. It has survived not only five\r\n' +
44-
'# centuries, but also the leap into electronic typesetting,\r\n' +
45-
'# remaining essentially unchanged. It was popularised in the\r\n' +
46-
'# 1960s with the release of Letraset sheets containing Lorem\r\n' +
47-
'# Ipsum passages, and more recently with desktop publishing\r\n' +
48-
'# software like Aldus PageMaker including versions of Lorem\r\n' +
49-
'# Ipsum.',
50-
},
51-
];
52-
const largeFileHex =
53-
':020000040003F7\n' +
54-
':10C98000FE600E746573745F66696C655F322E704D\n' +
55-
':10C990007923204C6F72656D20497073756D206925\n' +
56-
':10C9A000732073696D706C792064756D6D79207476\n' +
57-
':10C9B000657874206F6620746865207072696E7483\n' +
58-
':10C9C000696E6720616E640D0A2320747970657347\n' +
59-
':10C9D000657474696E6720696E6475737472792EFC\n' +
60-
':10C9E000204C6F72656D20497073756D206861739E\n' +
61-
':10C9F000206265656E2074686520696E6475732DAC\n' +
62-
':10CA00002C74727927730D0A23207374616E64612C\n' +
63-
':10CA100072642064756D6D79207465787420657614\n' +
64-
':10CA200065722073696E63652074686520313530E6\n' +
65-
':10CA300030732C207768656E20616E20756E6B6E8A\n' +
66-
':10CA40006F776E0D0A23207072696E7465722074A0\n' +
67-
':10CA50006F6F6B20612067616C6C6579206F662059\n' +
68-
':10CA60007479706520616E6420736372616D626CAD\n' +
69-
':10CA7000656420697420746F206D616B6520612E80\n' +
70-
':10CA80002D0D0A2320747970652073706563696DBC\n' +
71-
':10CA9000656E20626F6F6B2E204974206861732071\n' +
72-
':10CAA0007375727669766564206E6F74206F6E6C34\n' +
73-
':10CAB0007920666976650D0A232063656E74757248\n' +
74-
':10CAC0006965732C2062757420616C736F207468C3\n' +
75-
':10CAD00065206C65617020696E746F20656C65639C\n' +
76-
':10CAE00074726F6E696320747970657365747469AC\n' +
77-
':10CAF0006E672C0D0A232072656D61696E696E2F59\n' +
78-
':10CB00002E6720657373656E7469616C6C7920752E\n' +
79-
':10CB10006E6368616E6765642E2049742077617367\n' +
80-
':10CB200020706F70756C61726973656420696E2026\n' +
81-
':10CB30007468650D0A2320313936307320776974A3\n' +
82-
':10CB400068207468652072656C65617365206F6626\n' +
83-
':10CB5000204C6574726173657420736865657473C5\n' +
84-
':10CB600020636F6E7461696E696E67204C6F7265C9\n' +
85-
':10CB70006D0D0A2320497073756D207061737330D9\n' +
86-
':10CB80002F616765732C20616E64206D6F72652064\n' +
87-
':10CB9000726563656E746C7920776974682064656A\n' +
88-
':10CBA000736B746F70207075626C697368696E67FF\n' +
89-
':10CBB0000D0A2320736F667477617265206C696B50\n' +
90-
':10CBC0006520416C64757320506167654D616B65CC\n' +
91-
':10CBD0007220696E636C7564696E67207665727326\n' +
92-
':10CBE000696F6E73206F66204C6F72656D0D0A233E\n' +
93-
':10CBF00020497073756D2E0D0A23204C6F726531BC\n' +
94-
':10CC0000306D20497073756D2069732073696D7084\n' +
95-
':10CC10006C792064756D6D792074657874206F6609\n' +
96-
':10CC200020746865207072696E74696E6720616E29\n' +
97-
':10CC3000640D0A23207479706573657474696E6776\n' +
98-
':10CC400020696E6475737472792E204C6F72656DF5\n' +
99-
':10CC500020497073756D20686173206265656E2070\n' +
100-
':10CC600074686520696E64757374727927730D0A30\n' +
101-
':10CC700023207374616E646172642064756D6D321B\n' +
102-
':10CC80003179207465787420657665722073696ED9\n' +
103-
':10CC90006365207468652031353030732C207768E7\n' +
104-
':10CCA000656E20616E20756E6B6E6F776E0D0A2358\n' +
105-
':10CCB000207072696E74657220746F6F6B206120D2\n' +
106-
':10CCC00067616C6C6579206F66207479706520618E\n' +
107-
':10CCD0006E6420736372616D626C65642069742098\n' +
108-
':10CCE000746F206D616B6520610D0A23207479706B\n' +
109-
':10CCF000652073706563696D656E20626F6F6B335D\n' +
110-
':10CD0000322E2049742068617320737572766976BB\n' +
111-
':10CD10006564206E6F74206F6E6C7920666976652D\n' +
112-
':10CD20000D0A232063656E7475726965732C206229\n' +
113-
':10CD3000757420616C736F20746865206C65617018\n' +
114-
':10CD400020696E746F20656C656374726F6E6963C1\n' +
115-
':10CD5000207479706573657474696E672C0D0A238D\n' +
116-
':10CD60002072656D61696E696E6720657373656EAB\n' +
117-
':10CD70007469616C6C7920756E6368616E67653487\n' +
118-
':10CD800033642E2049742077617320706F70756C46\n' +
119-
':10CD900061726973656420696E207468650D0A2389\n' +
120-
':10CDA00020313936307320776974682074686520C3\n' +
121-
':10CDB00072656C65617365206F66204C6574726185\n' +
122-
':10CDC0007365742073686565747320636F6E746136\n' +
123-
':10CDD000696E696E67204C6F72656D0D0A2320497C\n' +
124-
':10CDE0007073756D2070617373616765732C20615A\n' +
125-
':10CDF0006E64206D6F726520726563656E746C354C\n' +
126-
':10CE000034792077697468206465736B746F70205F\n' +
127-
':10CE10007075626C697368696E670D0A2320736FA1\n' +
128-
':10CE2000667477617265206C696B6520416C64750E\n' +
129-
':10CE30007320506167654D616B657220696E636C2C\n' +
130-
':10CE40007564696E672076657273696F6E73206FA3\n' +
131-
':10CE500066204C6F72656D0D0A2320497073756DE5\n' +
132-
':10CE60002EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3\n' +
133-
':00000001FF';
134-
const largeMap = MemoryMap.fromHex(largeFileHex);
135-
const largeData = largeMap.get(0x3c980);
136-
const shortFileHex =
137-
':020000040003F7\n' +
138-
':10C90000FE3F0E746573745F66696C655F312E70EF\n' +
139-
':10C910007966726F6D206D6963726F6269742069E8\n' +
140-
':10C920006D706F727420646973706C61790D0A6444\n' +
141-
':10C930006973706C61792E73686F7728277827295F\n' +
142-
':10C94000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7\n' +
143-
':10C95000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE7\n' +
144-
':10C96000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD7\n' +
145-
':10C97000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7\n' +
146-
':00000001FF';
147-
const shortMap = MemoryMap.fromHex(shortFileHex);
148-
const shortData = shortMap.get(0x3c900);
20+
const files = [
21+
{
22+
fileName: 'test_file_1.py',
23+
fileStr: "from microbit import display\r\ndisplay.show('x')",
24+
},
25+
{
26+
fileName: 'test_file_2.py',
27+
fileStr:
28+
'# Lorem Ipsum is simply dummy text of the printing and\r\n' +
29+
"# typesetting industry. Lorem Ipsum has been the industry's\r\n" +
30+
'# standard dummy text ever since the 1500s, when an unknown\r\n' +
31+
'# printer took a galley of type and scrambled it to make a\r\n' +
32+
'# type specimen book. It has survived not only five\r\n' +
33+
'# centuries, but also the leap into electronic typesetting,\r\n' +
34+
'# remaining essentially unchanged. It was popularised in the\r\n' +
35+
'# 1960s with the release of Letraset sheets containing Lorem\r\n' +
36+
'# Ipsum passages, and more recently with desktop publishing\r\n' +
37+
'# software like Aldus PageMaker including versions of Lorem\r\n' +
38+
'# Ipsum.\r\n' +
39+
'# Lorem Ipsum is simply dummy text of the printing and\r\n' +
40+
"# typesetting industry. Lorem Ipsum has been the industry's\r\n" +
41+
'# standard dummy text ever since the 1500s, when an unknown\r\n' +
42+
'# printer took a galley of type and scrambled it to make a\r\n' +
43+
'# type specimen book. It has survived not only five\r\n' +
44+
'# centuries, but also the leap into electronic typesetting,\r\n' +
45+
'# remaining essentially unchanged. It was popularised in the\r\n' +
46+
'# 1960s with the release of Letraset sheets containing Lorem\r\n' +
47+
'# Ipsum passages, and more recently with desktop publishing\r\n' +
48+
'# software like Aldus PageMaker including versions of Lorem\r\n' +
49+
'# Ipsum.',
50+
},
51+
];
52+
const largeFileHex =
53+
':020000040003F7\n' +
54+
':10C98000FE600E746573745F66696C655F322E704D\n' +
55+
':10C990007923204C6F72656D20497073756D206925\n' +
56+
':10C9A000732073696D706C792064756D6D79207476\n' +
57+
':10C9B000657874206F6620746865207072696E7483\n' +
58+
':10C9C000696E6720616E640D0A2320747970657347\n' +
59+
':10C9D000657474696E6720696E6475737472792EFC\n' +
60+
':10C9E000204C6F72656D20497073756D206861739E\n' +
61+
':10C9F000206265656E2074686520696E6475732DAC\n' +
62+
':10CA00002C74727927730D0A23207374616E64612C\n' +
63+
':10CA100072642064756D6D79207465787420657614\n' +
64+
':10CA200065722073696E63652074686520313530E6\n' +
65+
':10CA300030732C207768656E20616E20756E6B6E8A\n' +
66+
':10CA40006F776E0D0A23207072696E7465722074A0\n' +
67+
':10CA50006F6F6B20612067616C6C6579206F662059\n' +
68+
':10CA60007479706520616E6420736372616D626CAD\n' +
69+
':10CA7000656420697420746F206D616B6520612E80\n' +
70+
':10CA80002D0D0A2320747970652073706563696DBC\n' +
71+
':10CA9000656E20626F6F6B2E204974206861732071\n' +
72+
':10CAA0007375727669766564206E6F74206F6E6C34\n' +
73+
':10CAB0007920666976650D0A232063656E74757248\n' +
74+
':10CAC0006965732C2062757420616C736F207468C3\n' +
75+
':10CAD00065206C65617020696E746F20656C65639C\n' +
76+
':10CAE00074726F6E696320747970657365747469AC\n' +
77+
':10CAF0006E672C0D0A232072656D61696E696E2F59\n' +
78+
':10CB00002E6720657373656E7469616C6C7920752E\n' +
79+
':10CB10006E6368616E6765642E2049742077617367\n' +
80+
':10CB200020706F70756C61726973656420696E2026\n' +
81+
':10CB30007468650D0A2320313936307320776974A3\n' +
82+
':10CB400068207468652072656C65617365206F6626\n' +
83+
':10CB5000204C6574726173657420736865657473C5\n' +
84+
':10CB600020636F6E7461696E696E67204C6F7265C9\n' +
85+
':10CB70006D0D0A2320497073756D207061737330D9\n' +
86+
':10CB80002F616765732C20616E64206D6F72652064\n' +
87+
':10CB9000726563656E746C7920776974682064656A\n' +
88+
':10CBA000736B746F70207075626C697368696E67FF\n' +
89+
':10CBB0000D0A2320736F667477617265206C696B50\n' +
90+
':10CBC0006520416C64757320506167654D616B65CC\n' +
91+
':10CBD0007220696E636C7564696E67207665727326\n' +
92+
':10CBE000696F6E73206F66204C6F72656D0D0A233E\n' +
93+
':10CBF00020497073756D2E0D0A23204C6F726531BC\n' +
94+
':10CC0000306D20497073756D2069732073696D7084\n' +
95+
':10CC10006C792064756D6D792074657874206F6609\n' +
96+
':10CC200020746865207072696E74696E6720616E29\n' +
97+
':10CC3000640D0A23207479706573657474696E6776\n' +
98+
':10CC400020696E6475737472792E204C6F72656DF5\n' +
99+
':10CC500020497073756D20686173206265656E2070\n' +
100+
':10CC600074686520696E64757374727927730D0A30\n' +
101+
':10CC700023207374616E646172642064756D6D321B\n' +
102+
':10CC80003179207465787420657665722073696ED9\n' +
103+
':10CC90006365207468652031353030732C207768E7\n' +
104+
':10CCA000656E20616E20756E6B6E6F776E0D0A2358\n' +
105+
':10CCB000207072696E74657220746F6F6B206120D2\n' +
106+
':10CCC00067616C6C6579206F66207479706520618E\n' +
107+
':10CCD0006E6420736372616D626C65642069742098\n' +
108+
':10CCE000746F206D616B6520610D0A23207479706B\n' +
109+
':10CCF000652073706563696D656E20626F6F6B335D\n' +
110+
':10CD0000322E2049742068617320737572766976BB\n' +
111+
':10CD10006564206E6F74206F6E6C7920666976652D\n' +
112+
':10CD20000D0A232063656E7475726965732C206229\n' +
113+
':10CD3000757420616C736F20746865206C65617018\n' +
114+
':10CD400020696E746F20656C656374726F6E6963C1\n' +
115+
':10CD5000207479706573657474696E672C0D0A238D\n' +
116+
':10CD60002072656D61696E696E6720657373656EAB\n' +
117+
':10CD70007469616C6C7920756E6368616E67653487\n' +
118+
':10CD800033642E2049742077617320706F70756C46\n' +
119+
':10CD900061726973656420696E207468650D0A2389\n' +
120+
':10CDA00020313936307320776974682074686520C3\n' +
121+
':10CDB00072656C65617365206F66204C6574726185\n' +
122+
':10CDC0007365742073686565747320636F6E746136\n' +
123+
':10CDD000696E696E67204C6F72656D0D0A2320497C\n' +
124+
':10CDE0007073756D2070617373616765732C20615A\n' +
125+
':10CDF0006E64206D6F726520726563656E746C354C\n' +
126+
':10CE000034792077697468206465736B746F70205F\n' +
127+
':10CE10007075626C697368696E670D0A2320736FA1\n' +
128+
':10CE2000667477617265206C696B6520416C64750E\n' +
129+
':10CE30007320506167654D616B657220696E636C2C\n' +
130+
':10CE40007564696E672076657273696F6E73206FA3\n' +
131+
':10CE500066204C6F72656D0D0A2320497073756DE5\n' +
132+
':10CE60002EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA3\n' +
133+
':00000001FF';
134+
const largeMap = MemoryMap.fromHex(largeFileHex);
135+
const largeData = largeMap.get(0x3c980);
136+
const shortFileHex =
137+
':020000040003F7\n' +
138+
':10C90000FE3F0E746573745F66696C655F312E70EF\n' +
139+
':10C910007966726F6D206D6963726F6269742069E8\n' +
140+
':10C920006D706F727420646973706C61790D0A6444\n' +
141+
':10C930006973706C61792E73686F7728277827295F\n' +
142+
':10C94000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7\n' +
143+
':10C95000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE7\n' +
144+
':10C96000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD7\n' +
145+
':10C97000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7\n' +
146+
':00000001FF';
147+
const shortMap = MemoryMap.fromHex(shortFileHex);
148+
const shortData = shortMap.get(0x3c900);
149149

150-
testResetFileSystem();
150+
it('Add files to hex.', () => {
151151
const filesObj: { [filename: string]: Uint8Array } = {};
152152
filesObj[files[0].fileName] = strToBytes(files[0].fileStr);
153153
filesObj[files[1].fileName] = strToBytes(files[1].fileStr);
154154

155+
testResetFileSystem();
155156
const fwWithFsOther = addIntelHexFiles(uPyHexFile, filesObj);
156157
// fs.writeFileSync('./ignore/output2.hex', fwWithFsOther);
157158

@@ -562,7 +563,7 @@ describe('Reading files from the filesystem.', () => {
562563
// TODO: Read tests with a file that has chunks in non-continuous order
563564
// TODO: Read test with chunks that point to each other in an infinite loop
564565
// TODO: Read test with chunks that don't point to each other in Marker/Tail
565-
// TODO: Read test with chunks using the all the start file markers
566+
// TODO: Read test with chunks using all the start file markers
566567
});
567568

568569
describe('Calculate sizes.', () => {
@@ -573,5 +574,33 @@ describe('Calculate sizes.', () => {
573574
expect(totalSize).toEqual(27 * 1024);
574575
});
575576

576-
// TODO: Test calculateFileSize()
577+
it('Calculate the space ocupied for a file in the fs.', () => {
578+
const fileSizeOne = calculateFileSize(
579+
'one_chunk.txt',
580+
new Uint8Array([30, 31, 32, 33, 34])
581+
);
582+
const fileSizeAlmostFullOne = calculateFileSize(
583+
'almost_one_chunk____.txt',
584+
new Uint8Array(99).fill(0x35)
585+
);
586+
const fileSizeOneOverflow = calculateFileSize(
587+
'one_chunk_overflow__.txt',
588+
new Uint8Array(100).fill(0x35)
589+
);
590+
const fileSizeTwo = calculateFileSize(
591+
'just_about_2_chunks_.txt',
592+
new Uint8Array(101).fill(0x35)
593+
);
594+
const fileSizeNine = calculateFileSize(
595+
'9_chunks.txt',
596+
new Uint8Array(1100).fill(0x35)
597+
);
598+
599+
// Real size counts chunks, so always a multiple of 128
600+
expect(fileSizeOne).toEqual(128);
601+
expect(fileSizeAlmostFullOne).toEqual(128);
602+
expect(fileSizeOneOverflow).toEqual(128 * 2);
603+
expect(fileSizeTwo).toEqual(128 * 2);
604+
expect(fileSizeNine).toEqual(128 * 9);
605+
});
577606
});

0 commit comments

Comments
 (0)