Begin versioning.
[fits.git] / src / nom / tam / util / test / BigFileTest.java
1 package nom.tam.util.test;
2
3 import org.junit.Test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
6
7 import nom.tam.util.BufferedFile;
8 import nom.tam.util.BufferedDataInputStream;
9 import java.io.FileInputStream;
10
11 public class BigFileTest {
12
13     @Test
14     public void test() throws Exception {
15         try {
16             // First create a 3 GB file.
17             String fname = System.getenv("BIGFILETEST");
18             if (fname == null) {
19                 System.out.println("BIGFILETEST environment not set.  Returning without test");
20                 return;
21             }
22             System.out.println("Big file test.  Takes quite a while.");
23             byte[] buf = new byte[100000000]; // 100 MB
24             BufferedFile bf = new BufferedFile(fname, "rw");
25             byte sample = 13;
26
27             for (int i = 0; i < 30; i += 1) {
28                 bf.write(buf);  // 30 x 100 MB = 3 GB.
29                 if (i == 24) {
30                     bf.write(new byte[]{sample});
31                 } // Add a marker.
32             }
33             bf.close();
34
35             // Now try to skip within the file.
36             bf = new BufferedFile(fname, "r");
37             long skip = 2500000000L; // 2.5 G
38
39             long val1 = bf.skipBytes(skip);
40             long val2 = bf.getFilePointer();
41             int val = bf.read();
42             bf.close();
43
44             assertEquals("SkipResult", skip, val1);
45             assertEquals("SkipPos", skip, val2);
46             assertEquals("SkipVal", (int) sample, val);
47
48             BufferedDataInputStream bdis = new BufferedDataInputStream(
49                     new FileInputStream(fname));
50             val1 = bdis.skipBytes(skip);
51             val = bdis.read();
52             bdis.close();
53             assertEquals("SSkipResult", skip, val1);
54             assertEquals("SSkipVal", (int) sample, val);
55         } catch (Exception e) {
56             e.printStackTrace(System.err);
57             throw e;
58         }
59     }
60 }