From 53e47bc101cad1d2fd1d8d1225de377187f1c449 Mon Sep 17 00:00:00 2001 From: Nahiyan Kamal Date: Thu, 9 Oct 2025 16:47:19 +0200 Subject: [PATCH 1/2] chore: Renaming --- src/main/java/prog/Main.java | 52 ++-- src/main/java/prog/MainFrame.java | 8 +- src/main/java/prog/MainPanel.java | 40 ++-- .../{Hzipping.java => HuffmanCompressor.java} | 222 ++++++++--------- ...nzipping.java => HuffmanDecompressor.java} | 224 +++++++++--------- src/main/java/prog/huffman/HuffmanUtils.java | 24 +- .../lzw/{Lzipping.java => LzwCompressor.java} | 96 ++++---- .../{Lunzipping.java => LzwDecompressor.java} | 68 +++--- src/test/java/prog/lzw/LunzippingTest.java | 1 - 9 files changed, 367 insertions(+), 368 deletions(-) rename src/main/java/prog/huffman/{Hzipping.java => HuffmanCompressor.java} (58%) rename src/main/java/prog/huffman/{Hunzipping.java => HuffmanDecompressor.java} (58%) rename src/main/java/prog/lzw/{Lzipping.java => LzwCompressor.java} (63%) rename src/main/java/prog/lzw/{Lunzipping.java => LzwDecompressor.java} (60%) diff --git a/src/main/java/prog/Main.java b/src/main/java/prog/Main.java index 46ec9db..93b6f4e 100644 --- a/src/main/java/prog/Main.java +++ b/src/main/java/prog/Main.java @@ -10,44 +10,44 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; -import prog.huffman.Hzipping; -import prog.huffman.Hunzipping; -import prog.lzw.Lzipping; -import prog.lzw.Lunzipping; +import prog.huffman.HuffmanCompressor; +import prog.huffman.HuffmanDecompressor; +import prog.lzw.LzwCompressor; +import prog.lzw.LzwDecompressor; public class Main extends JFrame implements ActionListener { // Definition of global values and items that are part of the GUI. - static public File opened_file, other_file; - static long past, future; - static JLabel redLabel, blueLabel, redScore, blueScore; + static public File openedFile, outputFile; + static long originalSize, compressedSize; + static JLabel originalSizeLabel, compressedSizeLabel, originalSizeValue, compressedSizeValue; static JPanel buttonPanel, titlePanel, scorePanel; - static JButton ZH, UH, ZL, UL, EX; + static JButton huffmanCompressButton, huffmanDecompressButton, lzwCompressButton, lzwDecompressButton, exitButton; public JPanel createContentPane() { return MainPanel.createContentPane(this); } private void handleHuffmanCompression() { - Hzipping.beginHzipping(opened_file.getPath()); + HuffmanCompressor.beginHuffmanCompression(openedFile.getPath()); showCompressionCompleteDialog("Zipping"); updateFileStats(".huffz"); } private void handleHuffmanDecompression() { - Hunzipping.beginHunzipping(opened_file.getPath()); + HuffmanDecompressor.beginHuffmanDecompression(openedFile.getPath()); showCompressionCompleteDialog("UnZipping"); updateDecompressionStats(); } private void handleLZWCompression() { - Lzipping.beginLzipping(opened_file.getPath()); + LzwCompressor.beginLzwCompression(openedFile.getPath()); showCompressionCompleteDialog("Zipping"); updateFileStats(".LmZWp"); } private void handleLZWDecompression() { - Lunzipping.beginLunzipping(opened_file.getPath()); + LzwDecompressor.beginLzwDecompression(openedFile.getPath()); showCompressionCompleteDialog("UnZipping"); updateDecompressionStats(); } @@ -59,31 +59,31 @@ private void showCompressionCompleteDialog(String operation) { } private void updateFileStats(String extension) { - redScore.setText(opened_file.length() + "Bytes"); - other_file = new File(opened_file.getPath() + extension); - future = other_file.length(); - blueScore.setText(future + "Bytes"); + originalSizeValue.setText(openedFile.length() + "Bytes"); + outputFile = new File(openedFile.getPath() + extension); + compressedSize = outputFile.length(); + compressedSizeValue.setText(compressedSize + "Bytes"); } private void updateDecompressionStats() { - redScore.setText(opened_file.length() + "Bytes"); - String s = opened_file.getPath(); + originalSizeValue.setText(openedFile.length() + "Bytes"); + String s = openedFile.getPath(); s = s.substring(0, s.length() - 6); - other_file = new File(s); - future = other_file.length(); - blueScore.setText(future + "Bytes"); + outputFile = new File(s); + compressedSize = outputFile.length(); + compressedSizeValue.setText(compressedSize + "Bytes"); } public void actionPerformed(ActionEvent e) { - if (e.getSource() == ZH) { + if (e.getSource() == huffmanCompressButton) { handleHuffmanCompression(); - } else if (e.getSource() == UH) { + } else if (e.getSource() == huffmanDecompressButton) { handleHuffmanDecompression(); - } else if (e.getSource() == ZL) { + } else if (e.getSource() == lzwCompressButton) { handleLZWCompression(); - } else if (e.getSource() == UL) { + } else if (e.getSource() == lzwDecompressButton) { handleLZWDecompression(); - } else if (e.getSource() == EX) { + } else if (e.getSource() == exitButton) { System.exit(0); } } diff --git a/src/main/java/prog/MainFrame.java b/src/main/java/prog/MainFrame.java index e42587b..294c426 100644 --- a/src/main/java/prog/MainFrame.java +++ b/src/main/java/prog/MainFrame.java @@ -44,10 +44,10 @@ private static void handleFileOpen() { fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { - Main.opened_file = fileChooser.getSelectedFile(); - Main.past = Main.opened_file.length(); - Main.redScore.setText(Main.past + "Bytes"); - Main.blueScore.setText("NotYetCalculated"); + Main.openedFile = fileChooser.getSelectedFile(); + Main.originalSize = Main.openedFile.length(); + Main.originalSizeValue.setText(Main.originalSize + "Bytes"); + Main.compressedSizeValue.setText("NotYetCalculated"); } } diff --git a/src/main/java/prog/MainPanel.java b/src/main/java/prog/MainPanel.java index aabebd3..b059f4b 100644 --- a/src/main/java/prog/MainPanel.java +++ b/src/main/java/prog/MainPanel.java @@ -31,11 +31,11 @@ private static void createTitlePanel(JPanel mainPanel) { mainPanel.add(Main.titlePanel); // Create and add labels - Main.redLabel = createLabel("Selected File Size: ", 43, 0, 150, 30); - Main.blueLabel = createLabel("After zip/unzip the file size: ", 10, 30, 170, 30); - - Main.titlePanel.add(Main.redLabel); - Main.titlePanel.add(Main.blueLabel); + Main.originalSizeLabel = createLabel("Selected File Size: ", 43, 0, 150, 30); + Main.compressedSizeLabel = createLabel("After zip/unzip the file size: ", 10, 30, 170, 30); + + Main.titlePanel.add(Main.originalSizeLabel); + Main.titlePanel.add(Main.compressedSizeLabel); } private static void createScorePanel(JPanel mainPanel) { @@ -46,11 +46,11 @@ private static void createScorePanel(JPanel mainPanel) { mainPanel.add(Main.scorePanel); // Create and add score labels - Main.redScore = createLabel("", 0, 0, 100, 30); - Main.blueScore = createLabel("", 0, 30, 100, 30); - - Main.scorePanel.add(Main.redScore); - Main.scorePanel.add(Main.blueScore); + Main.originalSizeValue = createLabel("", 0, 0, 100, 30); + Main.compressedSizeValue = createLabel("", 0, 30, 100, 30); + + Main.scorePanel.add(Main.originalSizeValue); + Main.scorePanel.add(Main.compressedSizeValue); } private static JLabel createLabel(String text, int x, int y, int width, int height) { @@ -77,18 +77,18 @@ private static void createButtonPanel(JPanel mainPanel, ActionListener listener) mainPanel.add(Main.buttonPanel); // Create compression buttons - Main.ZH = createButton("ZIP HuffZ", 0, 0, 120, 30, listener); - Main.UH = createButton("UNZIP HuffZ", 130, 0, 120, 30, listener); - Main.ZL = createButton("ZIP LmZWp", 260, 0, 120, 30, listener); - Main.UL = createButton("UNZIP LmZWp", 390, 0, 120, 30, listener); - Main.EX = createButton("EXIT", 130, 70, 250, 30, listener); + Main.huffmanCompressButton = createButton("ZIP HuffZ", 0, 0, 120, 30, listener); + Main.huffmanDecompressButton = createButton("UNZIP HuffZ", 130, 0, 120, 30, listener); + Main.lzwCompressButton = createButton("ZIP LmZWp", 260, 0, 120, 30, listener); + Main.lzwDecompressButton = createButton("UNZIP LmZWp", 390, 0, 120, 30, listener); + Main.exitButton = createButton("EXIT", 130, 70, 250, 30, listener); // Add buttons to panel - Main.buttonPanel.add(Main.ZH); - Main.buttonPanel.add(Main.UH); - Main.buttonPanel.add(Main.ZL); - Main.buttonPanel.add(Main.UL); - Main.buttonPanel.add(Main.EX); + Main.buttonPanel.add(Main.huffmanCompressButton); + Main.buttonPanel.add(Main.huffmanDecompressButton); + Main.buttonPanel.add(Main.lzwCompressButton); + Main.buttonPanel.add(Main.lzwDecompressButton); + Main.buttonPanel.add(Main.exitButton); } public static JPanel createContentPane(ActionListener listener) { diff --git a/src/main/java/prog/huffman/Hzipping.java b/src/main/java/prog/huffman/HuffmanCompressor.java similarity index 58% rename from src/main/java/prog/huffman/Hzipping.java rename to src/main/java/prog/huffman/HuffmanCompressor.java index 281ff72..2020249 100644 --- a/src/main/java/prog/huffman/Hzipping.java +++ b/src/main/java/prog/huffman/HuffmanCompressor.java @@ -11,54 +11,54 @@ import java.util.PriorityQueue; //if the frequency of a byte is more than 2^32 then there will be problem -public class Hzipping { +public class HuffmanCompressor { - static PriorityQueue pq = new PriorityQueue(); - static int[] freq = new int[300]; - static String[] ss = new String[300]; - static int exbits; - static byte bt; - static int cnt; // number of different characters + static PriorityQueue priorityQueue = new PriorityQueue(); + static int[] frequency = new int[300]; + static String[] huffmanCodes = new String[300]; + static int extraBits; + static byte currentByte; + static int uniqueCharCount; // number of different characters // for keeping frequncies of all the bytes // main tree class - static class TREE implements Comparable { - TREE Lchild; - TREE Rchild; - public String deb; - public int Bite; - public int Freqnc; + static class HuffmanNode implements Comparable { + HuffmanNode leftChild; + HuffmanNode rightChild; + public String code; + public int byteValue; + public int frequency; - public int compareTo(TREE T) { - if (this.Freqnc < T.Freqnc) + public int compareTo(HuffmanNode other) { + if (this.frequency < other.frequency) return -1; - if (this.Freqnc > T.Freqnc) + if (this.frequency > other.frequency) return 1; return 0; } } - static TREE Root; + static HuffmanNode root; /******************************************************************************* - * calculating frequence of file fname + * calculating frequence of file filename ******************************************************************************/ - public static void CalFreq(String fname) { + public static void calculateFrequency(String filename) { File file = null; - Byte bt; + Byte currentByte; - file = new File(fname); + file = new File(filename); try { FileInputStream file_input = new FileInputStream(file); DataInputStream data_in = new DataInputStream(file_input); while (true) { try { - bt = data_in.readByte(); - freq[HuffmanUtils.to(bt)]++; + currentByte = data_in.readByte(); + frequency[HuffmanUtils.to(currentByte)]++; } catch (EOFException eof) { System.out.println("End of File"); break; @@ -93,32 +93,32 @@ public static int to(Byte b) { /********************************************************************************** * freing the memory *********************************************************************************/ - public static void initHzipping() { + public static void initHuffmanCompressor() { int i; - cnt = 0; - if (Root != null) - HuffmanUtils.fredfs(Root, HuffmanUtils.HZIPPING_TREE_ACCESSOR); + uniqueCharCount = 0; + if (root != null) + HuffmanUtils.fredfs(root, HuffmanUtils.HZIPPING_TREE_ACCESSOR); for (i = 0; i < 300; i++) - freq[i] = 0; + frequency[i] = 0; for (i = 0; i < 300; i++) - ss[i] = ""; - pq.clear(); + huffmanCodes[i] = ""; + priorityQueue.clear(); } /**********************************************************************************/ /********************************************************************************** * dfs to free memory *********************************************************************************/ - public static void fredfs(TREE now) { + public static void fredfs(HuffmanNode node) { - if (now.Lchild == null && now.Rchild == null) { - now = null; + if (node.leftChild == null && node.rightChild == null) { + node = null; return; } - if (now.Lchild != null) - fredfs(now.Lchild); - if (now.Rchild != null) - fredfs(now.Rchild); + if (node.leftChild != null) + fredfs(node.leftChild); + if (node.rightChild != null) + fredfs(node.rightChild); } /**********************************************************************************/ @@ -126,16 +126,16 @@ public static void fredfs(TREE now) { /********************************************************************************** * dfs to make the codes *********************************************************************************/ - public static void dfs(TREE now, String st) { - now.deb = st; - if ((now.Lchild == null) && (now.Rchild == null)) { - ss[now.Bite] = st; + public static void generateHuffmanCodes(HuffmanNode node, String code) { + node.code = code; + if ((node.leftChild == null) && (node.rightChild == null)) { + huffmanCodes[node.byteValue] = code; return; } - if (now.Lchild != null) - dfs(now.Lchild, st + "0"); - if (now.Rchild != null) - dfs(now.Rchild, st + "1"); + if (node.leftChild != null) + generateHuffmanCodes(node.leftChild, code + "0"); + if (node.rightChild != null) + generateHuffmanCodes(node.rightChild, code + "1"); } /**********************************************************************************/ @@ -143,30 +143,30 @@ public static void dfs(TREE now, String st) { /******************************************************************************* * Making all the nodes in a priority Q making the tree *******************************************************************************/ - public static void MakeNode() { + public static void buildHuffmanTree() { int i; - pq.clear(); + priorityQueue.clear(); for (i = 0; i < 300; i++) { - if (freq[i] != 0) { - TREE Temp = new TREE(); - Temp.Bite = i; - Temp.Freqnc = freq[i]; - Temp.Lchild = null; - Temp.Rchild = null; - pq.add(Temp); - cnt++; + if (frequency[i] != 0) { + HuffmanNode Temp = new HuffmanNode(); + Temp.byteValue = i; + Temp.frequency = frequency[i]; + Temp.leftChild = null; + Temp.rightChild = null; + priorityQueue.add(Temp); + uniqueCharCount++; } } - TREE Temp1, Temp2; + HuffmanNode Temp1, Temp2; - if (cnt == 0) { + if (uniqueCharCount == 0) { return; - } else if (cnt == 1) { + } else if (uniqueCharCount == 1) { for (i = 0; i < 300; i++) - if (freq[i] != 0) { - ss[i] = "0"; + if (frequency[i] != 0) { + huffmanCodes[i] = "0"; break; } return; @@ -174,16 +174,16 @@ public static void MakeNode() { // will there b a problem if the file is empty // a bug is found if there is only one character - while (pq.size() != 1) { - TREE Temp = new TREE(); - Temp1 = pq.poll(); - Temp2 = pq.poll(); - Temp.Lchild = Temp1; - Temp.Rchild = Temp2; - Temp.Freqnc = Temp1.Freqnc + Temp2.Freqnc; - pq.add(Temp); + while (priorityQueue.size() != 1) { + HuffmanNode Temp = new HuffmanNode(); + Temp1 = priorityQueue.poll(); + Temp2 = priorityQueue.poll(); + Temp.leftChild = Temp1; + Temp.rightChild = Temp2; + Temp.frequency = Temp1.frequency + Temp2.frequency; + priorityQueue.add(Temp); } - Root = pq.poll(); + root = priorityQueue.poll(); } /*******************************************************************************/ @@ -191,18 +191,18 @@ public static void MakeNode() { /******************************************************************************* * encrypting *******************************************************************************/ - public static void encrypt(String fname) { + public static void encrypt(String filename) { File file = null; - file = new File(fname); + file = new File(filename); try { FileInputStream file_input = new FileInputStream(file); DataInputStream data_in = new DataInputStream(file_input); while (true) { try { - bt = data_in.readByte(); - freq[bt]++; + currentByte = data_in.readByte(); + frequency[currentByte]++; } catch (EOFException eof) { System.out.println("End of File"); break; @@ -223,12 +223,12 @@ public static void encrypt(String fname) { * fake zip creates a file "fakezip.txt" where puts the final binary codes * of the real zipped file *******************************************************************************/ - public static void fakezip(String fname) { + public static void fakezip(String filename) { File filei, fileo; int i; - filei = new File(fname); + filei = new File(filename); fileo = new File("fakezipped.txt"); try { FileInputStream file_input = new FileInputStream(filei); @@ -237,8 +237,8 @@ public static void fakezip(String fname) { while (true) { try { - bt = data_in.readByte(); - ps.print(ss[to(bt)]); + currentByte = data_in.readByte(); + ps.print(huffmanCodes[to(currentByte)]); } catch (EOFException eof) { System.out.println("End of File"); break; @@ -260,15 +260,15 @@ public static void fakezip(String fname) { /*******************************************************************************/ /******************************************************************************* - * real zip according to codes of fakezip.txt (fname) + * real zip according to codes of fakezip.txt (filename) *******************************************************************************/ - public static void realzip(String fname, String fname1) { + public static void realzip(String filename, String filename1) { File filei, fileo; int i, j = 10; - Byte btt; + Byte currentBytet; - filei = new File(fname); - fileo = new File(fname1); + filei = new File(filename); + fileo = new File(filename1); try { FileInputStream file_input = new FileInputStream(filei); @@ -276,42 +276,42 @@ public static void realzip(String fname, String fname1) { FileOutputStream file_output = new FileOutputStream(fileo); DataOutputStream data_out = new DataOutputStream(file_output); - data_out.writeInt(cnt); + data_out.writeInt(uniqueCharCount); for (i = 0; i < 256; i++) { - if (freq[i] != 0) { - btt = (byte) i; - data_out.write(btt); - data_out.writeInt(freq[i]); + if (frequency[i] != 0) { + currentBytet = (byte) i; + data_out.write(currentBytet); + data_out.writeInt(frequency[i]); } } - long texbits; - texbits = filei.length() % 8; - texbits = (8 - texbits) % 8; - exbits = (int) texbits; - data_out.writeInt(exbits); + long textraBits; + textraBits = filei.length() % 8; + textraBits = (8 - textraBits) % 8; + extraBits = (int) textraBits; + data_out.writeInt(extraBits); while (true) { try { - bt = 0; + currentByte = 0; byte ch; - for (exbits = 0; exbits < 8; exbits++) { + for (extraBits = 0; extraBits < 8; extraBits++) { ch = data_in.readByte(); - bt *= 2; + currentByte *= 2; if (ch == '1') - bt++; + currentByte++; } - data_out.write(bt); + data_out.write(currentByte); } catch (EOFException eof) { int x; - if (exbits != 0) { - for (x = exbits; x < 8; x++) { - bt *= 2; + if (extraBits != 0) { + for (x = extraBits; x < 8; x++) { + currentByte *= 2; } - data_out.write(bt); + data_out.write(currentByte); } - exbits = (int) texbits; - System.out.println("extrabits: " + exbits); + extraBits = (int) textraBits; + System.out.println("extrabits: " + extraBits); System.out.println("End of File"); break; } @@ -335,7 +335,7 @@ public static void realzip(String fname, String fname1) { /* * public static void main (String[] args) { initHzipping(); * CalFreq("in.txt"); // calculate the frequency of each digit MakeNode(); - * // makeing corresponding nodes if(cnt>1) dfs(Root,""); // dfs to make the + * // makeing corresponding nodes if(uniqueCharCount>1) generateHuffmanCodes(root,""); // dfs to make the * codes fakezip("in.txt"); // fake zip file which will have the binary of * the input to fakezipped.txt file * realzip("fakezipped.txt","in.txt"+".huffz"); // making the real zip @@ -344,17 +344,17 @@ public static void realzip(String fname, String fname1) { * } */ - public static void beginHzipping(String arg1) { - initHzipping(); - CalFreq(arg1); // calculate the frequency of each digit - MakeNode(); // makeing corresponding nodes - if (cnt > 1) - dfs(Root, ""); // dfs to make the codes + public static void beginHuffmanCompression(String arg1) { + initHuffmanCompressor(); + calculateFrequency(arg1); // calculate the frequency of each digit + buildHuffmanTree(); // build huffman tree from frequencies + if (uniqueCharCount > 1) + generateHuffmanCodes(root, ""); // dfs to make the codes fakezip(arg1); // fake zip file which will have the binary of the input // to fakezipped.txt file realzip("fakezipped.txt", arg1 + ".huffz"); // making the real zip // according the fakezip.txt // file - initHzipping(); + initHuffmanCompressor(); } } \ No newline at end of file diff --git a/src/main/java/prog/huffman/Hunzipping.java b/src/main/java/prog/huffman/HuffmanDecompressor.java similarity index 58% rename from src/main/java/prog/huffman/Hunzipping.java rename to src/main/java/prog/huffman/HuffmanDecompressor.java index c1a0f84..f774442 100644 --- a/src/main/java/prog/huffman/Hunzipping.java +++ b/src/main/java/prog/huffman/HuffmanDecompressor.java @@ -9,70 +9,70 @@ import java.io.IOException; import java.util.PriorityQueue; -public class Hunzipping { - static PriorityQueue pq1 = new PriorityQueue(); - static int[] freq1 = new int[300]; - static String[] ss1 = new String[300]; // INT TO CODE - static String[] btost = new String[300]; // INT TO BIN - static String bigone; // THE BIG STRING - static String temp; // TEMPORARY STRING - static int exbits1; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP +public class HuffmanDecompressor { + static PriorityQueue priorityQueue = new PriorityQueue(); + static int[] frequency = new int[300]; + static String[] huffmanCodes = new String[300]; // INT TO CODE + static String[] byteToString = new String[300]; // INT TO BIN + static String bitBuffer; // THE BIG STRING + static String tempCode; // TEMPORARY STRING + static int extraBits; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP // CODE MULTIPLE OF 8 - static int putit; // - static int cntu; // NUMBER OF freqs available - - static class TREE implements Comparable { - TREE Lchild; - TREE Rchild; - public String deb; - public int Bite; - public int freq1nc; - - public int compareTo(TREE T) { - if (this.freq1nc < T.freq1nc) + static int decodedByte; // + static int uniqueCharCount; // NUMBER OF freqs available + + static class HuffmanNode implements Comparable { + HuffmanNode leftChild; + HuffmanNode rightChild; + public String code; + public int byteValue; + public int frequency; + + public int compareTo(HuffmanNode other) { + if (this.frequency < other.frequency) return -1; - if (this.freq1nc > T.freq1nc) + if (this.frequency > other.frequency) return 1; return 0; } } - static TREE Root; + static HuffmanNode root; /********************************************************************************** * freing the memory *********************************************************************************/ - public static void initHunzipping() { + public static void initHuffmanDecompressor() { int i; - if (Root != null) - HuffmanUtils.fredfs(Root, HuffmanUtils.HUNZIPPING_TREE_ACCESSOR); + if (root != null) + HuffmanUtils.fredfs(root, HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR); for (i = 0; i < 300; i++) - freq1[i] = 0; + frequency[i] = 0; for (i = 0; i < 300; i++) - ss1[i] = ""; - pq1.clear(); - bigone = ""; // THE BIG STRING - temp = ""; // TEMPORARY STRING - exbits1 = 0; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP CODE + huffmanCodes[i] = ""; + priorityQueue.clear(); + bitBuffer = ""; // THE BIG STRING + tempCode = ""; // TEMPORARY STRING + extraBits = 0; // EXTRA BITS ADDED AT THE LAST TO MAKE THE FINAL ZIP CODE // MULTIPLE OF 8 - putit = 0; // - cntu = 0; + decodedByte = 0; // + uniqueCharCount = 0; } /**********************************************************************************/ /********************************************************************************** * dfs1 to free memory *********************************************************************************/ - public static void fredfs1(TREE now) { + public static void fredfs1(HuffmanNode node) { - if (now.Lchild == null && now.Rchild == null) { - now = null; + if (node.leftChild == null && node.rightChild == null) { + node = null; return; } - if (now.Lchild != null) - fredfs1(now.Lchild); - if (now.Rchild != null) - fredfs1(now.Rchild); + if (node.leftChild != null) + fredfs1(node.leftChild); + if (node.rightChild != null) + fredfs1(node.rightChild); } /**********************************************************************************/ @@ -80,17 +80,17 @@ public static void fredfs1(TREE now) { /********************************************************************************** * dfs1 to make the codes *********************************************************************************/ - // public static void dfs1(TREE now,int code,String st) - public static void dfs1(TREE now, String st) { - now.deb = st; - if ((now.Lchild == null) && (now.Rchild == null)) { - ss1[now.Bite] = st; + // public static void generateHuffmanCodes(HuffmanNode node,int code,String code) + public static void generateHuffmanCodes(HuffmanNode node, String code) { + node.code = code; + if ((node.leftChild == null) && (node.rightChild == null)) { + huffmanCodes[node.byteValue] = code; return; } - if (now.Lchild != null) - dfs1(now.Lchild, st + "0"); - if (now.Rchild != null) - dfs1(now.Rchild, st + "1"); + if (node.leftChild != null) + generateHuffmanCodes(node.leftChild, code + "0"); + if (node.rightChild != null) + generateHuffmanCodes(node.rightChild, code + "1"); } /**********************************************************************************/ @@ -98,30 +98,30 @@ public static void dfs1(TREE now, String st) { /******************************************************************************* * Making all the nodes in a priority Q making the tree *******************************************************************************/ - public static void MakeNode1() { + public static void buildHuffmanTree() { int i; - cntu = 0; + uniqueCharCount = 0; for (i = 0; i < 300; i++) { - if (freq1[i] != 0) { - - TREE Temp = new TREE(); - Temp.Bite = i; - Temp.freq1nc = freq1[i]; - Temp.Lchild = null; - Temp.Rchild = null; - pq1.add(Temp); - cntu++; + if (frequency[i] != 0) { + + HuffmanNode Temp = new HuffmanNode(); + Temp.byteValue = i; + Temp.frequency = frequency[i]; + Temp.leftChild = null; + Temp.rightChild = null; + priorityQueue.add(Temp); + uniqueCharCount++; } } - TREE Temp1, Temp2; + HuffmanNode Temp1, Temp2; - if (cntu == 0) { + if (uniqueCharCount == 0) { return; - } else if (cntu == 1) { + } else if (uniqueCharCount == 1) { for (i = 0; i < 300; i++) - if (freq1[i] != 0) { - ss1[i] = "0"; + if (frequency[i] != 0) { + huffmanCodes[i] = "0"; break; } return; @@ -129,16 +129,16 @@ public static void MakeNode1() { // will there b a problem if the file is empty // a bug is found if there is only one character - while (pq1.size() != 1) { - TREE Temp = new TREE(); - Temp1 = pq1.poll(); - Temp2 = pq1.poll(); - Temp.Lchild = Temp1; - Temp.Rchild = Temp2; - Temp.freq1nc = Temp1.freq1nc + Temp2.freq1nc; - pq1.add(Temp); + while (priorityQueue.size() != 1) { + HuffmanNode Temp = new HuffmanNode(); + Temp1 = priorityQueue.poll(); + Temp2 = priorityQueue.poll(); + Temp.leftChild = Temp1; + Temp.rightChild = Temp2; + Temp.frequency = Temp1.frequency + Temp2.frequency; + priorityQueue.add(Temp); } - Root = pq1.poll(); + root = priorityQueue.poll(); } /*******************************************************************************/ @@ -154,12 +154,12 @@ public static void readfreq1(String cc) { try { FileInputStream file_input = new FileInputStream(filei); DataInputStream data_in = new DataInputStream(file_input); - cntu = data_in.readInt(); + uniqueCharCount = data_in.readInt(); - for (i = 0; i < cntu; i++) { + for (i = 0; i < uniqueCharCount; i++) { baital = data_in.readByte(); fey = data_in.readInt(); - freq1[HuffmanUtils.to(baital)] = fey; + frequency[HuffmanUtils.to(baital)] = fey; } data_in.close(); file_input.close(); @@ -167,13 +167,13 @@ public static void readfreq1(String cc) { System.out.println("IO exception = " + e); } - MakeNode1(); // makeing corresponding nodes - if (cntu > 1) - dfs1(Root, ""); // dfs1 to make the codes + buildHuffmanTree(); // makeing corresponding nodes + if (uniqueCharCount > 1) + generateHuffmanCodes(root, ""); // dfs1 to make the codes for (i = 0; i < 256; i++) { - if (ss1[i] == null) - ss1[i] = ""; + if (huffmanCodes[i] == null) + huffmanCodes[i] = ""; } filei = null; } @@ -187,37 +187,37 @@ public static void createbin() { int i, j; String t; for (i = 0; i < 256; i++) { - btost[i] = ""; + byteToString[i] = ""; j = i; while (j != 0) { if (j % 2 == 1) - btost[i] += "1"; + byteToString[i] += "1"; else - btost[i] += "0"; + byteToString[i] += "0"; j /= 2; } t = ""; - for (j = btost[i].length() - 1; j >= 0; j--) { - t += btost[i].charAt(j); + for (j = byteToString[i].length() - 1; j >= 0; j--) { + t += byteToString[i].charAt(j); } - btost[i] = t; - // System.out.println(btost[i]); + byteToString[i] = t; + // System.out.println(byteToString[i]); } - btost[0] = "0"; + byteToString[0] = "0"; } /****************************************************************************/ /****************************************************************************** - * got yes means temp is a valid code and putit is the code's corresponding + * got yes means tempCode is a valid code and decodedByte is the code's corresponding * val ******************************************************************************/ public static int got() { int i; for (i = 0; i < 256; i++) { - if (ss1[i].compareTo(temp) == 0) { - putit = i; + if (huffmanCodes[i].compareTo(tempCode) == 0) { + decodedByte = i; return 1; } } @@ -266,7 +266,7 @@ public static void readbin(String zip, String unz) { int ok, bt; Byte b; int j, i; - bigone = ""; + bitBuffer = ""; f1 = new File(zip); f2 = new File(unz); try { @@ -275,16 +275,16 @@ public static void readbin(String zip, String unz) { FileInputStream file_input = new FileInputStream(f1); DataInputStream data_in = new DataInputStream(file_input); try { - cntu = data_in.readInt(); - System.out.println(cntu); - for (i = 0; i < cntu; i++) { + uniqueCharCount = data_in.readInt(); + System.out.println(uniqueCharCount); + for (i = 0; i < uniqueCharCount; i++) { b = data_in.readByte(); j = data_in.readInt(); // System.out.println(ss[to(b)]); } - exbits1 = data_in.readInt(); - System.out.println(exbits1); + extraBits = data_in.readInt(); + System.out.println(extraBits); } catch (EOFException eof) { System.out.println("End of File"); @@ -294,24 +294,24 @@ public static void readbin(String zip, String unz) { try { b = data_in.readByte(); bt = HuffmanUtils.to(b); - bigone += HuffmanUtils.makeeight(btost[bt]); + bitBuffer += HuffmanUtils.makeeight(byteToString[bt]); - // System.out.println(bigone); + // System.out.println(bitBuffer); while (true) { ok = 1; - temp = ""; - for (i = 0; i < bigone.length() - exbits1; i++) { - temp += bigone.charAt(i); - // System.out.println(temp); + tempCode = ""; + for (i = 0; i < bitBuffer.length() - extraBits; i++) { + tempCode += bitBuffer.charAt(i); + // System.out.println(tempCode); if (got() == 1) { - data_out.write(putit); + data_out.write(decodedByte); ok = 0; String s = ""; - for (j = temp.length(); j < bigone.length(); j++) { - s += bigone.charAt(j); + for (j = tempCode.length(); j < bitBuffer.length(); j++) { + s += bitBuffer.charAt(j); } - bigone = s; + bitBuffer = s; break; } } @@ -339,14 +339,14 @@ public static void readbin(String zip, String unz) { /************************************************************************************/ // again improve the to function // error if only <=one character in the input file - public static void beginHunzipping(String arg1) { - initHunzipping(); + public static void beginHuffmanDecompression(String arg1) { + initHuffmanDecompressor(); readfreq1(arg1); createbin(); int n = arg1.length(); String arg2 = arg1.substring(0, n - 6); readbin(arg1, arg2); - initHunzipping(); + initHuffmanDecompressor(); } /* diff --git a/src/main/java/prog/huffman/HuffmanUtils.java b/src/main/java/prog/huffman/HuffmanUtils.java index 12ba015..dc8bb0a 100644 --- a/src/main/java/prog/huffman/HuffmanUtils.java +++ b/src/main/java/prog/huffman/HuffmanUtils.java @@ -34,23 +34,23 @@ public interface TreeAccessor { T getRightChild(T node); } - // Accessor for Hzipping.TREE - public static final TreeAccessor HZIPPING_TREE_ACCESSOR = new TreeAccessor() { - public Hzipping.TREE getLeftChild(Hzipping.TREE node) { - return node.Lchild; + // Accessor for HuffmanCompressor.HuffmanNode + public static final TreeAccessor HZIPPING_TREE_ACCESSOR = new TreeAccessor() { + public HuffmanCompressor.HuffmanNode getLeftChild(HuffmanCompressor.HuffmanNode node) { + return node.leftChild; } - public Hzipping.TREE getRightChild(Hzipping.TREE node) { - return node.Rchild; + public HuffmanCompressor.HuffmanNode getRightChild(HuffmanCompressor.HuffmanNode node) { + return node.rightChild; } }; - // Accessor for Hunzipping.TREE - public static final TreeAccessor HUNZIPPING_TREE_ACCESSOR = new TreeAccessor() { - public Hunzipping.TREE getLeftChild(Hunzipping.TREE node) { - return node.Lchild; + // Accessor for HuffmanDecompressor.HuffmanNode + public static final TreeAccessor HUNZIPPING_HuffmanNode_ACCESSOR = new TreeAccessor() { + public HuffmanDecompressor.HuffmanNode getLeftChild(HuffmanDecompressor.HuffmanNode node) { + return node.leftChild; } - public Hunzipping.TREE getRightChild(Hunzipping.TREE node) { - return node.Rchild; + public HuffmanDecompressor.HuffmanNode getRightChild(HuffmanDecompressor.HuffmanNode node) { + return node.rightChild; } }; diff --git a/src/main/java/prog/lzw/Lzipping.java b/src/main/java/prog/lzw/LzwCompressor.java similarity index 63% rename from src/main/java/prog/lzw/Lzipping.java rename to src/main/java/prog/lzw/LzwCompressor.java index b7a67e1..669597d 100644 --- a/src/main/java/prog/lzw/Lzipping.java +++ b/src/main/java/prog/lzw/LzwCompressor.java @@ -11,33 +11,33 @@ import java.util.HashMap; import java.util.Map; -public class Lzipping { +public class LzwCompressor { - public static int btsz; - public static String big; + public static int bitSize; + public static String bitBuffer; /* * ========================================================================= - * = make the integer to bin then return btsz size's string + * = make the integer to bin then return bitSize size's string * ========================================================================= */ - public static String fil(int inp) { + public static String intToBinaryString(int input) { String ret = "", r1 = ""; - if (inp == 0) + if (input == 0) ret = "0"; int i; - while (inp != 0) { - if ((inp % 2) == 1) + while (input != 0) { + if ((input % 2) == 1) ret += "1"; else ret += "0"; - inp /= 2; + input /= 2; } for (i = ret.length() - 1; i >= 0; i--) { r1 += ret.charAt(i); } - while (r1.length() != btsz) { + while (r1.length() != bitSize) { r1 = "0" + r1; } return r1; @@ -54,13 +54,13 @@ public static String fil(int inp) { * = */ - public static Byte strtobt(String in) { + public static Byte stringToByte(String binaryString) { - int i, n = in.length(); + int i, n = binaryString.length(); byte ret = 0; for (i = 0; i < n; i++) { ret *= 2.; - if (in.charAt(i) == '1') + if (binaryString.charAt(i) == '1') ret++; } for (; n < 8; n++) @@ -78,7 +78,7 @@ public static Byte strtobt(String in) { * = precalcs the length of the * ========================================================================= */ - public static void precalc(String fileis) { + public static void calculateBitSize(String filename) { Map dictionary = new HashMap(); int dictSize = 256; for (int i = 0; i < 256; i++) @@ -87,11 +87,11 @@ public static void precalc(String fileis) { String w = ""; File filei = null; - filei = new File(fileis); + filei = new File(filename); try { - FileInputStream file_input = new FileInputStream(filei); - DataInputStream data_in = new DataInputStream(file_input); + FileInputStream file_inputut = new FileInputStream(filei); + DataInputStream data_in = new DataInputStream(file_inputut); Byte c; int ch; @@ -114,7 +114,7 @@ public static void precalc(String fileis) { break; } } - file_input.close(); + file_inputut.close(); data_in.close(); } catch (IOException e) { System.out.println("IO exception = " + e); @@ -122,13 +122,13 @@ public static void precalc(String fileis) { // if empty file if (dictSize <= 1) { - btsz = 1; + bitSize = 1; } else { - btsz = 0; + bitSize = 0; long i = 1; while (i < dictSize) { i *= 2; - btsz++; + bitSize++; } } filei = null; @@ -146,26 +146,26 @@ public static void precalc(String fileis) { * ==== */ - public static void Lamzip(String fileis) { + public static void compressFile(String filename) { Map dictionary = new HashMap(); int dictSize = 256; - big = ""; + bitBuffer = ""; for (int i = 0; i < 256; i++) dictionary.put("" + (char) i, i); int mpsz = 256; String w = ""; - String fileos = fileis + ".LmZWp"; + String fileos = filename + ".LmZWp"; File filei, fileo; - filei = new File(fileis); + filei = new File(filename); fileo = new File(fileos); try { - FileInputStream file_input = new FileInputStream(filei); - DataInputStream data_in = new DataInputStream(file_input); + FileInputStream file_inputut = new FileInputStream(filei); + DataInputStream data_in = new DataInputStream(file_inputut); FileOutputStream file_output = new FileOutputStream(fileo); DataOutputStream data_out = new DataOutputStream(file_output); - data_out.writeInt(btsz); + data_out.writeInt(bitSize); Byte c; int ch; while (true) { @@ -177,10 +177,10 @@ public static void Lamzip(String fileis) { if (dictionary.containsKey(wc)) w = wc; else { - big += fil(dictionary.get(w)); - while (big.length() >= 8) { - data_out.write(strtobt(big.substring(0, 8))); - big = big.substring(8, big.length()); + bitBuffer += intToBinaryString(dictionary.get(w)); + while (bitBuffer.length() >= 8) { + data_out.write(stringToByte(bitBuffer.substring(0, 8))); + bitBuffer = bitBuffer.substring(8, bitBuffer.length()); } if (mpsz < 100000) { @@ -196,18 +196,18 @@ public static void Lamzip(String fileis) { } if (!w.equals("")) { - big += fil(dictionary.get(w)); - while (big.length() >= 8) { - data_out.write(strtobt(big.substring(0, 8))); - big = big.substring(8, big.length()); + bitBuffer += intToBinaryString(dictionary.get(w)); + while (bitBuffer.length() >= 8) { + data_out.write(stringToByte(bitBuffer.substring(0, 8))); + bitBuffer = bitBuffer.substring(8, bitBuffer.length()); } - if (big.length() >= 1) { - data_out.write(strtobt(big)); + if (bitBuffer.length() >= 1) { + data_out.write(stringToByte(bitBuffer)); } } data_in.close(); data_out.close(); - file_input.close(); + file_inputut.close(); file_output.close(); } catch (IOException e) { System.out.println("IO exception = " + e); @@ -222,17 +222,17 @@ public static void Lamzip(String fileis) { * ==== */ - public static void beginLzipping(String arg1) { - btsz = 0; - big = ""; - precalc(arg1); - Lamzip(arg1); - btsz = 0; - big = ""; + public static void beginLzwCompression(String arg1) { + bitSize = 0; + bitBuffer = ""; + calculateBitSize(arg1); + compressFile(arg1); + bitSize = 0; + bitBuffer = ""; } /* - * public static void main(String[] args) { btsz=0; big=""; - * precalc("in.txt"); Lamzip("in.txt"); btsz=0; big=""; } + * public static void main(String[] args) { bitSize=0; bitBuffer=""; + * precalc("binaryString.txt"); Lamzip("binaryString.txt"); bitSize=0; bitBuffer=""; } */ } \ No newline at end of file diff --git a/src/main/java/prog/lzw/Lunzipping.java b/src/main/java/prog/lzw/LzwDecompressor.java similarity index 60% rename from src/main/java/prog/lzw/Lunzipping.java rename to src/main/java/prog/lzw/LzwDecompressor.java index d91a38f..7140569 100644 --- a/src/main/java/prog/lzw/Lunzipping.java +++ b/src/main/java/prog/lzw/LzwDecompressor.java @@ -11,49 +11,49 @@ import java.util.HashMap; import java.util.Map; -public class Lunzipping { +public class LzwDecompressor { - public static int bitsz1; + public static int bitSize; // byte er string representation in eight digit - public static String bttost[] = new String[256]; - public static String big1; + public static String byteToString[] = new String[256]; + public static String bitBuffer; /* * ============================================================ make all the * binary to string conversion for 8 bits * ============================================================= */ - public static void pre() { + public static void initializeByteToStringTable() { int i, j; String r1; - bttost[0] = "0"; + byteToString[0] = "0"; for (i = 0; i < 256; i++) { r1 = ""; j = i; if (i != 0) - bttost[i] = ""; + byteToString[i] = ""; while (j != 0) { if ((j % 2) == 1) - bttost[i] += "1"; + byteToString[i] += "1"; else - bttost[i] += "0"; + byteToString[i] += "0"; j /= 2; } - for (j = bttost[i].length() - 1; j >= 0; j--) { - r1 += bttost[i].charAt(j); + for (j = byteToString[i].length() - 1; j >= 0; j--) { + r1 += byteToString[i].charAt(j); } while (r1.length() < 8) { r1 = "0" + r1; } - bttost[i] = r1; + byteToString[i] = r1; } } /* * ========================================================================= */ - public static void Lunzip(String fileis) { + public static void decompressFile(String filename) { int k; int dictSize = 256; int mpsz = 256; @@ -62,10 +62,10 @@ public static void Lunzip(String fileis) { for (int i = 0; i < 256; i++) dictionary.put(i, "" + (char) i); - String fileos = fileis.substring(0, fileis.length() - 6); + String fileos = filename.substring(0, filename.length() - 6); File filei = null, fileo = null; - filei = new File(fileis); + filei = new File(filename); fileo = new File(fileos); try { FileInputStream file_input = new FileInputStream(filei); @@ -74,13 +74,13 @@ public static void Lunzip(String fileis) { DataOutputStream data_out = new DataOutputStream(file_output); Byte c; - bitsz1 = data_in.readInt(); + bitSize = data_in.readInt(); while (true) { try { c = data_in.readByte(); - big1 += bttost[CommonUtil.byteToUnsignedInt(c)]; - if (big1.length() >= bitsz1) + bitBuffer += byteToString[CommonUtil.byteToUnsignedInt(c)]; + if (bitBuffer.length() >= bitSize) break; } catch (EOFException eof) { System.out.println("End of File"); @@ -88,9 +88,9 @@ public static void Lunzip(String fileis) { } } - if (big1.length() >= bitsz1) { - k = CommonUtil.binaryStringToInt(big1.substring(0, bitsz1)); - big1 = big1.substring(bitsz1, big1.length()); + if (bitBuffer.length() >= bitSize) { + k = CommonUtil.binaryStringToInt(bitBuffer.substring(0, bitSize)); + bitBuffer = bitBuffer.substring(bitSize, bitBuffer.length()); } else { data_in.close(); data_out.close(); @@ -104,12 +104,12 @@ public static void Lunzip(String fileis) { while (true) { try { - while (big1.length() < bitsz1) { + while (bitBuffer.length() < bitSize) { c = data_in.readByte(); - big1 += bttost[CommonUtil.byteToUnsignedInt(c)]; + bitBuffer += byteToString[CommonUtil.byteToUnsignedInt(c)]; } - k = CommonUtil.binaryStringToInt(big1.substring(0, bitsz1)); - big1 = big1.substring(bitsz1, big1.length()); + k = CommonUtil.binaryStringToInt(bitBuffer.substring(0, bitSize)); + bitBuffer = bitBuffer.substring(bitSize, bitBuffer.length()); String entry = ""; if (dictionary.containsKey(k)) { @@ -145,17 +145,17 @@ public static void Lunzip(String fileis) { } - public static void beginLunzipping(String arg1) { - big1 = ""; - bitsz1 = 0; - pre(); - Lunzip(arg1); - big1 = ""; - bitsz1 = 0; + public static void beginLzwDecompression(String arg1) { + bitBuffer = ""; + bitSize = 0; + initializeByteToStringTable(); + decompressFile(arg1); + bitBuffer = ""; + bitSize = 0; } /* - * public static void main(String[] args) { big1=""; bitsz1=0; pre(); - * Lunzip("in.txt.LmZWp"); big1=""; bitsz1=0; } + * public static void main(String[] args) { bitBuffer=""; bitSize=0; pre(); + * Lunzip("in.txt.LmZWp"); bitBuffer=""; bitSize=0; } */ } \ No newline at end of file diff --git a/src/test/java/prog/lzw/LunzippingTest.java b/src/test/java/prog/lzw/LunzippingTest.java index b3344d7..7cd7507 100644 --- a/src/test/java/prog/lzw/LunzippingTest.java +++ b/src/test/java/prog/lzw/LunzippingTest.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; From be2b3b4105ee41a72e1ca1781d51548dca340043 Mon Sep 17 00:00:00 2001 From: Nahiyan Kamal Date: Thu, 9 Oct 2025 17:14:01 +0200 Subject: [PATCH 2/2] test message --- src/test/java/prog/MainFrameTest.java | 18 ++-- src/test/java/prog/MainPanelTest.java | 88 +++++++++---------- .../HuffmanCompressionIntegrationTest.java | 8 +- ...ngTest.java => HuffmanCompressorTest.java} | 40 ++++----- ...Test.java => HuffmanDecompressorTest.java} | 54 ++++++------ .../java/prog/huffman/HuffmanUtilsTest.java | 82 ++++++++--------- .../lzw/LzwCompressionIntegrationTest.java | 4 +- ...ippingTest.java => LzwCompressorTest.java} | 52 +++++------ ...pingTest.java => LzwDecompressorTest.java} | 24 ++--- 9 files changed, 185 insertions(+), 185 deletions(-) rename src/test/java/prog/huffman/{HzippingTest.java => HuffmanCompressorTest.java} (68%) rename src/test/java/prog/huffman/{HunzippingTest.java => HuffmanDecompressorTest.java} (68%) rename src/test/java/prog/lzw/{LzippingTest.java => LzwCompressorTest.java} (71%) rename src/test/java/prog/lzw/{LunzippingTest.java => LzwDecompressorTest.java} (78%) diff --git a/src/test/java/prog/MainFrameTest.java b/src/test/java/prog/MainFrameTest.java index 624a3bc..216f6d4 100644 --- a/src/test/java/prog/MainFrameTest.java +++ b/src/test/java/prog/MainFrameTest.java @@ -105,15 +105,15 @@ void testFileOpenOperation() throws InterruptedException, IOException { assertTrue(latch.await(5, TimeUnit.SECONDS), "Frame initialization timed out"); // Simulate file selection - Main.opened_file = testFile; - Main.past = testFile.length(); - Main.redScore.setText(Main.past + "Bytes"); - Main.blueScore.setText("NotYetCalculated"); - - assertEquals(testFile, Main.opened_file, "Selected file should be set"); - assertEquals(testFile.length(), Main.past, "File size should be set"); - assertEquals(testFile.length() + "Bytes", Main.redScore.getText(), "Red score should show file size"); - assertEquals("NotYetCalculated", Main.blueScore.getText(), "Blue score should show not calculated"); + Main.openedFile = testFile; + Main.originalSize = testFile.length(); + Main.originalSizeValue.setText(Main.originalSize + "Bytes"); + Main.compressedSizeValue.setText("NotYetCalculated"); + + assertEquals(testFile, Main.openedFile, "Selected file should be set"); + assertEquals(testFile.length(), Main.originalSize, "File size should be set"); + assertEquals(testFile.length() + "Bytes", Main.originalSizeValue.getText(), "Original size should show file size"); + assertEquals("NotYetCalculated", Main.compressedSizeValue.getText(), "Compressed size should show not calculated"); SwingUtilities.invokeLater(() -> mainFrame.dispose()); } diff --git a/src/test/java/prog/MainPanelTest.java b/src/test/java/prog/MainPanelTest.java index 03e413f..695cbf8 100644 --- a/src/test/java/prog/MainPanelTest.java +++ b/src/test/java/prog/MainPanelTest.java @@ -40,14 +40,14 @@ void testTitlePanelProperties() { assertNotNull(Main.titlePanel, "Title panel should be created"); assertEquals(new Point(90, 20), Main.titlePanel.getLocation(), "Title panel location should match"); assertEquals(new Dimension(170, 70), Main.titlePanel.getSize(), "Title panel size should match"); - + // Test labels in title panel - assertEquals("Selected File Size: ", Main.redLabel.getText(), "Red label text should match"); - assertEquals("After zip/unzip the file size: ", Main.blueLabel.getText(), "Blue label text should match"); - + assertEquals("Selected File Size: ", Main.originalSizeLabel.getText(), "Original size label text should match"); + assertEquals("After zip/unzip the file size: ", Main.compressedSizeLabel.getText(), "Compressed size label text should match"); + // Test label alignment - assertEquals(SwingConstants.CENTER, Main.redLabel.getHorizontalAlignment(), "Red label should be center-aligned"); - assertEquals(SwingConstants.CENTER, Main.blueLabel.getHorizontalAlignment(), "Blue label should be center-aligned"); + assertEquals(SwingConstants.CENTER, Main.originalSizeLabel.getHorizontalAlignment(), "Original size label should be center-aligned"); + assertEquals(SwingConstants.CENTER, Main.compressedSizeLabel.getHorizontalAlignment(), "Compressed size label should be center-aligned"); } @Test @@ -55,14 +55,14 @@ void testScorePanelProperties() { assertNotNull(Main.scorePanel, "Score panel should be created"); assertEquals(new Point(270, 20), Main.scorePanel.getLocation(), "Score panel location should match"); assertEquals(new Dimension(120, 60), Main.scorePanel.getSize(), "Score panel size should match"); - + // Test score labels - assertEquals("", Main.redScore.getText(), "Red score should be empty initially"); - assertEquals("", Main.blueScore.getText(), "Blue score should be empty initially"); - + assertEquals("", Main.originalSizeValue.getText(), "Original size value should be empty initially"); + assertEquals("", Main.compressedSizeValue.getText(), "Compressed size value should be empty initially"); + // Test label alignment - assertEquals(SwingConstants.CENTER, Main.redScore.getHorizontalAlignment(), "Red score should be center-aligned"); - assertEquals(SwingConstants.CENTER, Main.blueScore.getHorizontalAlignment(), "Blue score should be center-aligned"); + assertEquals(SwingConstants.CENTER, Main.originalSizeValue.getHorizontalAlignment(), "Original size value should be center-aligned"); + assertEquals(SwingConstants.CENTER, Main.compressedSizeValue.getHorizontalAlignment(), "Compressed size value should be center-aligned"); } @Test @@ -76,52 +76,52 @@ void testButtonPanelProperties() { @Test void testButtonProperties() { // Test ZIP HuffZ button - assertEquals("ZIP HuffZ", Main.ZH.getText(), "ZIP HuffZ button text should match"); - assertEquals(new Point(0, 0), Main.ZH.getLocation(), "ZIP HuffZ button location should match"); - assertEquals(new Dimension(120, 30), Main.ZH.getSize(), "ZIP HuffZ button size should match"); + assertEquals("ZIP HuffZ", Main.huffmanCompressButton.getText(), "ZIP HuffZ button text should match"); + assertEquals(new Point(0, 0), Main.huffmanCompressButton.getLocation(), "ZIP HuffZ button location should match"); + assertEquals(new Dimension(120, 30), Main.huffmanCompressButton.getSize(), "ZIP HuffZ button size should match"); // Test UNZIP HuffZ button - assertEquals("UNZIP HuffZ", Main.UH.getText(), "UNZIP HuffZ button text should match"); - assertEquals(new Point(130, 0), Main.UH.getLocation(), "UNZIP HuffZ button location should match"); - assertEquals(new Dimension(120, 30), Main.UH.getSize(), "UNZIP HuffZ button size should match"); + assertEquals("UNZIP HuffZ", Main.huffmanDecompressButton.getText(), "UNZIP HuffZ button text should match"); + assertEquals(new Point(130, 0), Main.huffmanDecompressButton.getLocation(), "UNZIP HuffZ button location should match"); + assertEquals(new Dimension(120, 30), Main.huffmanDecompressButton.getSize(), "UNZIP HuffZ button size should match"); // Test ZIP LmZWp button - assertEquals("ZIP LmZWp", Main.ZL.getText(), "ZIP LmZWp button text should match"); - assertEquals(new Point(260, 0), Main.ZL.getLocation(), "ZIP LmZWp button location should match"); - assertEquals(new Dimension(120, 30), Main.ZL.getSize(), "ZIP LmZWp button size should match"); + assertEquals("ZIP LmZWp", Main.lzwCompressButton.getText(), "ZIP LmZWp button text should match"); + assertEquals(new Point(260, 0), Main.lzwCompressButton.getLocation(), "ZIP LmZWp button location should match"); + assertEquals(new Dimension(120, 30), Main.lzwCompressButton.getSize(), "ZIP LmZWp button size should match"); // Test UNZIP LmZWp button - assertEquals("UNZIP LmZWp", Main.UL.getText(), "UNZIP LmZWp button text should match"); - assertEquals(new Point(390, 0), Main.UL.getLocation(), "UNZIP LmZWp button location should match"); - assertEquals(new Dimension(120, 30), Main.UL.getSize(), "UNZIP LmZWp button size should match"); + assertEquals("UNZIP LmZWp", Main.lzwDecompressButton.getText(), "UNZIP LmZWp button text should match"); + assertEquals(new Point(390, 0), Main.lzwDecompressButton.getLocation(), "UNZIP LmZWp button location should match"); + assertEquals(new Dimension(120, 30), Main.lzwDecompressButton.getSize(), "UNZIP LmZWp button size should match"); // Test EXIT button - assertEquals("EXIT", Main.EX.getText(), "EXIT button text should match"); - assertEquals(new Point(130, 70), Main.EX.getLocation(), "EXIT button location should match"); - assertEquals(new Dimension(250, 30), Main.EX.getSize(), "EXIT button size should match"); + assertEquals("EXIT", Main.exitButton.getText(), "EXIT button text should match"); + assertEquals(new Point(130, 70), Main.exitButton.getLocation(), "EXIT button location should match"); + assertEquals(new Dimension(250, 30), Main.exitButton.getSize(), "EXIT button size should match"); } @Test void testButtonActionListeners() { // Verify that all buttons have action listeners attached - assertNotNull(Main.ZH.getActionListeners(), "ZIP HuffZ button should have action listener"); - assertNotNull(Main.UH.getActionListeners(), "UNZIP HuffZ button should have action listener"); - assertNotNull(Main.ZL.getActionListeners(), "ZIP LmZWp button should have action listener"); - assertNotNull(Main.UL.getActionListeners(), "UNZIP LmZWp button should have action listener"); - assertNotNull(Main.EX.getActionListeners(), "EXIT button should have action listener"); - - assertEquals(1, Main.ZH.getActionListeners().length, "ZIP HuffZ button should have exactly one listener"); - assertEquals(1, Main.UH.getActionListeners().length, "UNZIP HuffZ button should have exactly one listener"); - assertEquals(1, Main.ZL.getActionListeners().length, "ZIP LmZWp button should have exactly one listener"); - assertEquals(1, Main.UL.getActionListeners().length, "UNZIP LmZWp button should have exactly one listener"); - assertEquals(1, Main.EX.getActionListeners().length, "EXIT button should have exactly one listener"); - + assertNotNull(Main.huffmanCompressButton.getActionListeners(), "ZIP HuffZ button should have action listener"); + assertNotNull(Main.huffmanDecompressButton.getActionListeners(), "UNZIP HuffZ button should have action listener"); + assertNotNull(Main.lzwCompressButton.getActionListeners(), "ZIP LmZWp button should have action listener"); + assertNotNull(Main.lzwDecompressButton.getActionListeners(), "UNZIP LmZWp button should have action listener"); + assertNotNull(Main.exitButton.getActionListeners(), "EXIT button should have action listener"); + + assertEquals(1, Main.huffmanCompressButton.getActionListeners().length, "ZIP HuffZ button should have exactly one listener"); + assertEquals(1, Main.huffmanDecompressButton.getActionListeners().length, "UNZIP HuffZ button should have exactly one listener"); + assertEquals(1, Main.lzwCompressButton.getActionListeners().length, "ZIP LmZWp button should have exactly one listener"); + assertEquals(1, Main.lzwDecompressButton.getActionListeners().length, "UNZIP LmZWp button should have exactly one listener"); + assertEquals(1, Main.exitButton.getActionListeners().length, "EXIT button should have exactly one listener"); + // Verify that all buttons have the same listener instance - ActionListener zhListener = Main.ZH.getActionListeners()[0]; - assertSame(zhListener, Main.UH.getActionListeners()[0], "All buttons should share the same listener"); - assertSame(zhListener, Main.ZL.getActionListeners()[0], "All buttons should share the same listener"); - assertSame(zhListener, Main.UL.getActionListeners()[0], "All buttons should share the same listener"); - assertSame(zhListener, Main.EX.getActionListeners()[0], "All buttons should share the same listener"); + ActionListener zhListener = Main.huffmanCompressButton.getActionListeners()[0]; + assertSame(zhListener, Main.huffmanDecompressButton.getActionListeners()[0], "All buttons should share the same listener"); + assertSame(zhListener, Main.lzwCompressButton.getActionListeners()[0], "All buttons should share the same listener"); + assertSame(zhListener, Main.lzwDecompressButton.getActionListeners()[0], "All buttons should share the same listener"); + assertSame(zhListener, Main.exitButton.getActionListeners()[0], "All buttons should share the same listener"); } @Test diff --git a/src/test/java/prog/huffman/HuffmanCompressionIntegrationTest.java b/src/test/java/prog/huffman/HuffmanCompressionIntegrationTest.java index 9b0f995..e3a574a 100644 --- a/src/test/java/prog/huffman/HuffmanCompressionIntegrationTest.java +++ b/src/test/java/prog/huffman/HuffmanCompressionIntegrationTest.java @@ -38,7 +38,7 @@ void testLargeFileCompression() throws IOException { System.out.println("Original file size: " + originalSize + " bytes"); // Compress the file - Hzipping.beginHzipping(originalFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(originalFile.getAbsolutePath()); assertTrue(compressedFile.exists(), "Compressed file should exist"); long compressedSize = Files.size(compressedFile.toPath()); @@ -50,7 +50,7 @@ void testLargeFileCompression() throws IOException { assertFalse(originalFile.exists(), "Original file should be deleted"); // Decompress - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists(), "Decompressed file should exist"); // Compare contents @@ -92,7 +92,7 @@ private void testCompressionCycle(File testFile) throws IOException { String originalContent = Files.readString(testFile.toPath()); // Compress - Hzipping.beginHzipping(testFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(testFile.getAbsolutePath()); assertTrue(compressedFile.exists(), "Compressed file should exist"); // Delete original file @@ -100,7 +100,7 @@ private void testCompressionCycle(File testFile) throws IOException { assertFalse(testFile.exists(), "Original file should be deleted"); // Decompress - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists(), "Decompressed file should exist"); // Compare contents diff --git a/src/test/java/prog/huffman/HzippingTest.java b/src/test/java/prog/huffman/HuffmanCompressorTest.java similarity index 68% rename from src/test/java/prog/huffman/HzippingTest.java rename to src/test/java/prog/huffman/HuffmanCompressorTest.java index 30b45fc..0cab68d 100644 --- a/src/test/java/prog/huffman/HzippingTest.java +++ b/src/test/java/prog/huffman/HuffmanCompressorTest.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*; -class HzippingTest { +class HuffmanCompressorTest { @TempDir Path tempDir; private File inputFile; @@ -31,7 +31,7 @@ void testEmptyFile() throws IOException { writer.write(""); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); File compressedFile = new File(inputFile.getAbsolutePath() + ".huffz"); assertTrue(compressedFile.exists()); @@ -45,7 +45,7 @@ void testSingleCharacterFile() throws IOException { writer.write("aaaa"); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); File compressedFile = new File(inputFile.getAbsolutePath() + ".huffz"); assertTrue(compressedFile.exists()); @@ -59,7 +59,7 @@ void testMultipleCharacterFile() throws IOException { writer.write("Hello, World! This is a test file with multiple characters."); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); File compressedFile = new File(inputFile.getAbsolutePath() + ".huffz"); assertTrue(compressedFile.exists()); @@ -74,21 +74,21 @@ void testFrequencyCalculation() throws IOException { writer.write("aabbc"); } - Hzipping.initHzipping(); - Hzipping.CalFreq(inputFile.getAbsolutePath()); + HuffmanCompressor.initHuffmanCompressor(); + HuffmanCompressor.calculateFrequency(inputFile.getAbsolutePath()); // Check frequencies - assertEquals(2, Hzipping.freq['a']); - assertEquals(2, Hzipping.freq['b']); - assertEquals(1, Hzipping.freq['c']); + assertEquals(2, HuffmanCompressor.frequency['a']); + assertEquals(2, HuffmanCompressor.frequency['b']); + assertEquals(1, HuffmanCompressor.frequency['c']); } @Test void testByteConversion() { // Test positive byte - assertEquals(65, Hzipping.to((byte) 65)); // 'A' + assertEquals(65, HuffmanCompressor.to((byte) 65)); // 'A' // Test negative byte - assertEquals(128, Hzipping.to((byte) -128)); + assertEquals(128, HuffmanCompressor.to((byte) -128)); } @Test @@ -98,24 +98,24 @@ void testTreeConstruction() throws IOException { writer.write("aabbc"); } - Hzipping.initHzipping(); - Hzipping.CalFreq(inputFile.getAbsolutePath()); - Hzipping.MakeNode(); + HuffmanCompressor.initHuffmanCompressor(); + HuffmanCompressor.calculateFrequency(inputFile.getAbsolutePath()); + HuffmanCompressor.buildHuffmanTree(); // Verify tree construction - assertNotNull(Hzipping.Root); - assertEquals(5, Hzipping.Root.Freqnc); // Total frequency should be 5 + assertNotNull(HuffmanCompressor.root); + assertEquals(5, HuffmanCompressor.root.frequency); // Total frequency should be 5 } @Test void testInitialization() { - Hzipping.initHzipping(); + HuffmanCompressor.initHuffmanCompressor(); // Check if frequencies are reset - assertTrue(Arrays.stream(Hzipping.freq).allMatch(freq -> freq == 0)); + assertTrue(Arrays.stream(HuffmanCompressor.frequency).allMatch(freq -> freq == 0)); // Check if string array is reset - assertTrue(Arrays.stream(Hzipping.ss).allMatch(s -> s.equals(""))); + assertTrue(Arrays.stream(HuffmanCompressor.huffmanCodes).allMatch(s -> s.equals(""))); // Check if priority queue is empty - assertTrue(Hzipping.pq.isEmpty()); + assertTrue(HuffmanCompressor.priorityQueue.isEmpty()); } } \ No newline at end of file diff --git a/src/test/java/prog/huffman/HunzippingTest.java b/src/test/java/prog/huffman/HuffmanDecompressorTest.java similarity index 68% rename from src/test/java/prog/huffman/HunzippingTest.java rename to src/test/java/prog/huffman/HuffmanDecompressorTest.java index 01ad0ab..5562879 100644 --- a/src/test/java/prog/huffman/HunzippingTest.java +++ b/src/test/java/prog/huffman/HuffmanDecompressorTest.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*; -class HunzippingTest { +class HuffmanDecompressorTest { @TempDir Path tempDir; private File inputFile; @@ -35,11 +35,11 @@ void testEmptyFileUnzipping() throws IOException { writer.write(""); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Test unzipping - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists()); assertEquals(0, decompressedFile.length()); } @@ -52,11 +52,11 @@ void testSingleCharacterUnzipping() throws IOException { writer.write(content); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Test unzipping - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists()); String decompressedContent = Files.readString(decompressedFile.toPath()); @@ -71,11 +71,11 @@ void testMultipleCharacterUnzipping() throws IOException { writer.write(content); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Test unzipping - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists()); String decompressedContent = Files.readString(decompressedFile.toPath()); @@ -85,37 +85,37 @@ void testMultipleCharacterUnzipping() throws IOException { @Test void testByteConversion() { // Test positive byte - assertEquals(65, Hunzipping.to((byte) 65)); // 'A' + assertEquals(65, HuffmanDecompressor.to((byte) 65)); // 'A' // Test negative byte - assertEquals(128, Hunzipping.to((byte) -128)); + assertEquals(128, HuffmanDecompressor.to((byte) -128)); } @Test void testMakeEight() { // Test padding of binary strings to 8 bits - assertEquals("00000000", Hunzipping.makeeight("")); - assertEquals("00000001", Hunzipping.makeeight("1")); - assertEquals("00001111", Hunzipping.makeeight("1111")); - assertEquals("11111111", Hunzipping.makeeight("11111111")); + assertEquals("00000000", HuffmanDecompressor.makeeight("")); + assertEquals("00000001", HuffmanDecompressor.makeeight("1")); + assertEquals("00001111", HuffmanDecompressor.makeeight("1111")); + assertEquals("11111111", HuffmanDecompressor.makeeight("11111111")); } @Test void testInitialization() { // Test initialization of static variables - Hunzipping.initHunzipping(); - + HuffmanDecompressor.initHuffmanDecompressor(); + // Check if frequencies are reset - assertTrue(Arrays.stream(Hunzipping.freq1).allMatch(freq -> freq == 0)); + assertTrue(Arrays.stream(HuffmanDecompressor.frequency).allMatch(freq -> freq == 0)); // Check if string arrays are reset - assertTrue(Arrays.stream(Hunzipping.ss1).allMatch(s -> s == null || s.equals(""))); + assertTrue(Arrays.stream(HuffmanDecompressor.huffmanCodes).allMatch(s -> s == null || s.equals(""))); // Check if priority queue is empty - assertTrue(Hunzipping.pq1.isEmpty()); + assertTrue(HuffmanDecompressor.priorityQueue.isEmpty()); // Check if other variables are reset - assertEquals("", Hunzipping.bigone); - assertEquals("", Hunzipping.temp); - assertEquals(0, Hunzipping.exbits1); - assertEquals(0, Hunzipping.putit); - assertEquals(0, Hunzipping.cntu); + assertEquals("", HuffmanDecompressor.bitBuffer); + assertEquals("", HuffmanDecompressor.tempCode); + assertEquals(0, HuffmanDecompressor.extraBits); + assertEquals(0, HuffmanDecompressor.decodedByte); + assertEquals(0, HuffmanDecompressor.uniqueCharCount); } @Test @@ -130,11 +130,11 @@ void testLargeFileUnzipping() throws IOException { writer.write(content.toString()); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Test unzipping - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists()); String decompressedContent = Files.readString(decompressedFile.toPath()); @@ -149,11 +149,11 @@ void testSpecialCharactersUnzipping() throws IOException { writer.write(content); } - Hzipping.beginHzipping(inputFile.getAbsolutePath()); + HuffmanCompressor.beginHuffmanCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Test unzipping - Hunzipping.beginHunzipping(compressedFile.getAbsolutePath()); + HuffmanDecompressor.beginHuffmanDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists()); String decompressedContent = Files.readString(decompressedFile.toPath()); diff --git a/src/test/java/prog/huffman/HuffmanUtilsTest.java b/src/test/java/prog/huffman/HuffmanUtilsTest.java index 01b0e94..3f1b344 100644 --- a/src/test/java/prog/huffman/HuffmanUtilsTest.java +++ b/src/test/java/prog/huffman/HuffmanUtilsTest.java @@ -39,74 +39,74 @@ void testMakeEight() { @Test void testFreeDfsWithHzippingTree() { - // Create a simple Hzipping tree structure - Hzipping.TREE root = new Hzipping.TREE(); - Hzipping.TREE left = new Hzipping.TREE(); - Hzipping.TREE right = new Hzipping.TREE(); - - root.Lchild = left; - root.Rchild = right; - root.Bite = 1; - left.Bite = 2; - right.Bite = 3; + // Create a simple HuffmanCompressor tree structure + HuffmanCompressor.HuffmanNode root = new HuffmanCompressor.HuffmanNode(); + HuffmanCompressor.HuffmanNode left = new HuffmanCompressor.HuffmanNode(); + HuffmanCompressor.HuffmanNode right = new HuffmanCompressor.HuffmanNode(); + + root.leftChild = left; + root.rightChild = right; + root.byteValue = 1; + left.byteValue = 2; + right.byteValue = 3; // Test that no exception is thrown when traversing - assertDoesNotThrow(() -> + assertDoesNotThrow(() -> HuffmanUtils.fredfs(root, HuffmanUtils.HZIPPING_TREE_ACCESSOR) ); } @Test void testFreeDfsWithHunzippingTree() { - // Create a simple Hunzipping tree structure - Hunzipping.TREE root = new Hunzipping.TREE(); - Hunzipping.TREE left = new Hunzipping.TREE(); - Hunzipping.TREE right = new Hunzipping.TREE(); - - root.Lchild = left; - root.Rchild = right; - root.Bite = 1; - left.Bite = 2; - right.Bite = 3; + // Create a simple HuffmanDecompressor tree structure + HuffmanDecompressor.HuffmanNode root = new HuffmanDecompressor.HuffmanNode(); + HuffmanDecompressor.HuffmanNode left = new HuffmanDecompressor.HuffmanNode(); + HuffmanDecompressor.HuffmanNode right = new HuffmanDecompressor.HuffmanNode(); + + root.leftChild = left; + root.rightChild = right; + root.byteValue = 1; + left.byteValue = 2; + right.byteValue = 3; // Test that no exception is thrown when traversing - assertDoesNotThrow(() -> - HuffmanUtils.fredfs(root, HuffmanUtils.HUNZIPPING_TREE_ACCESSOR) + assertDoesNotThrow(() -> + HuffmanUtils.fredfs(root, HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR) ); } @Test void testTreeAccessors() { - // Test Hzipping tree accessor - Hzipping.TREE hzipRoot = new Hzipping.TREE(); - Hzipping.TREE hzipLeft = new Hzipping.TREE(); - Hzipping.TREE hzipRight = new Hzipping.TREE(); - hzipRoot.Lchild = hzipLeft; - hzipRoot.Rchild = hzipRight; + // Test HuffmanCompressor tree accessor + HuffmanCompressor.HuffmanNode hzipRoot = new HuffmanCompressor.HuffmanNode(); + HuffmanCompressor.HuffmanNode hzipLeft = new HuffmanCompressor.HuffmanNode(); + HuffmanCompressor.HuffmanNode hzipRight = new HuffmanCompressor.HuffmanNode(); + hzipRoot.leftChild = hzipLeft; + hzipRoot.rightChild = hzipRight; assertEquals(hzipLeft, HuffmanUtils.HZIPPING_TREE_ACCESSOR.getLeftChild(hzipRoot)); assertEquals(hzipRight, HuffmanUtils.HZIPPING_TREE_ACCESSOR.getRightChild(hzipRoot)); - // Test Hunzipping tree accessor - Hunzipping.TREE hunzipRoot = new Hunzipping.TREE(); - Hunzipping.TREE hunzipLeft = new Hunzipping.TREE(); - Hunzipping.TREE hunzipRight = new Hunzipping.TREE(); - hunzipRoot.Lchild = hunzipLeft; - hunzipRoot.Rchild = hunzipRight; + // Test HuffmanDecompressor tree accessor + HuffmanDecompressor.HuffmanNode hunzipRoot = new HuffmanDecompressor.HuffmanNode(); + HuffmanDecompressor.HuffmanNode hunzipLeft = new HuffmanDecompressor.HuffmanNode(); + HuffmanDecompressor.HuffmanNode hunzipRight = new HuffmanDecompressor.HuffmanNode(); + hunzipRoot.leftChild = hunzipLeft; + hunzipRoot.rightChild = hunzipRight; - assertEquals(hunzipLeft, HuffmanUtils.HUNZIPPING_TREE_ACCESSOR.getLeftChild(hunzipRoot)); - assertEquals(hunzipRight, HuffmanUtils.HUNZIPPING_TREE_ACCESSOR.getRightChild(hunzipRoot)); + assertEquals(hunzipLeft, HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR.getLeftChild(hunzipRoot)); + assertEquals(hunzipRight, HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR.getRightChild(hunzipRoot)); } @Test void testNullTreeNodes() { // Test with null children - Hzipping.TREE hzipRoot = new Hzipping.TREE(); + HuffmanCompressor.HuffmanNode hzipRoot = new HuffmanCompressor.HuffmanNode(); assertNull(HuffmanUtils.HZIPPING_TREE_ACCESSOR.getLeftChild(hzipRoot)); assertNull(HuffmanUtils.HZIPPING_TREE_ACCESSOR.getRightChild(hzipRoot)); - Hunzipping.TREE hunzipRoot = new Hunzipping.TREE(); - assertNull(HuffmanUtils.HUNZIPPING_TREE_ACCESSOR.getLeftChild(hunzipRoot)); - assertNull(HuffmanUtils.HUNZIPPING_TREE_ACCESSOR.getRightChild(hunzipRoot)); + HuffmanDecompressor.HuffmanNode hunzipRoot = new HuffmanDecompressor.HuffmanNode(); + assertNull(HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR.getLeftChild(hunzipRoot)); + assertNull(HuffmanUtils.HUNZIPPING_HuffmanNode_ACCESSOR.getRightChild(hunzipRoot)); } } \ No newline at end of file diff --git a/src/test/java/prog/lzw/LzwCompressionIntegrationTest.java b/src/test/java/prog/lzw/LzwCompressionIntegrationTest.java index dad8959..ec04328 100644 --- a/src/test/java/prog/lzw/LzwCompressionIntegrationTest.java +++ b/src/test/java/prog/lzw/LzwCompressionIntegrationTest.java @@ -34,7 +34,7 @@ private void testLzwCompressionCycle(String content) throws IOException { long originalSize = Files.size(originalFile.toPath()); // Compress using LZW - Lzipping.beginLzipping(originalFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(originalFile.getAbsolutePath()); assertTrue(compressedFile.exists(), "Compressed file should exist"); // Store original file content @@ -45,7 +45,7 @@ private void testLzwCompressionCycle(String content) throws IOException { assertFalse(originalFile.exists(), "Original file should be deleted"); // Decompress using LZW - Lunzipping.beginLunzipping(compressedFile.getAbsolutePath()); + LzwDecompressor.beginLzwDecompression(compressedFile.getAbsolutePath()); assertTrue(decompressedFile.exists(), "Decompressed file should exist"); // Compare contents diff --git a/src/test/java/prog/lzw/LzippingTest.java b/src/test/java/prog/lzw/LzwCompressorTest.java similarity index 71% rename from src/test/java/prog/lzw/LzippingTest.java rename to src/test/java/prog/lzw/LzwCompressorTest.java index c2ce3a2..074f8d8 100644 --- a/src/test/java/prog/lzw/LzippingTest.java +++ b/src/test/java/prog/lzw/LzwCompressorTest.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*; -class LzippingTest { +class LzwCompressorTest { @TempDir Path tempDir; private File inputFile; @@ -30,25 +30,25 @@ void setUp() throws IOException { @Test void testFillBinaryString() { // Test with btsz = 8 - Lzipping.btsz = 8; - assertEquals("00000000", Lzipping.fil(0)); - assertEquals("00000001", Lzipping.fil(1)); - assertEquals("00001010", Lzipping.fil(10)); - assertEquals("11111111", Lzipping.fil(255)); + LzwCompressor.bitSize = 8; + assertEquals("00000000", LzwCompressor.intToBinaryString(0)); + assertEquals("00000001", LzwCompressor.intToBinaryString(1)); + assertEquals("00001010", LzwCompressor.intToBinaryString(10)); + assertEquals("11111111", LzwCompressor.intToBinaryString(255)); // Test with btsz = 4 - Lzipping.btsz = 4; - assertEquals("0000", Lzipping.fil(0)); - assertEquals("0001", Lzipping.fil(1)); - assertEquals("1111", Lzipping.fil(15)); + LzwCompressor.bitSize = 4; + assertEquals("0000", LzwCompressor.intToBinaryString(0)); + assertEquals("0001", LzwCompressor.intToBinaryString(1)); + assertEquals("1111", LzwCompressor.intToBinaryString(15)); } @Test void testStringToByte() { - assertEquals((byte) 0, Lzipping.strtobt("00000000")); - assertEquals((byte) 1, Lzipping.strtobt("00000001")); - assertEquals((byte) -1, Lzipping.strtobt("11111111")); - assertEquals((byte) 65, Lzipping.strtobt("01000001")); // 'A' + assertEquals((byte) 0, LzwCompressor.stringToByte("00000000")); + assertEquals((byte) 1, LzwCompressor.stringToByte("00000001")); + assertEquals((byte) -1, LzwCompressor.stringToByte("11111111")); + assertEquals((byte) 65, LzwCompressor.stringToByte("01000001")); // 'A' } @Test @@ -58,7 +58,7 @@ void testEmptyFileCompression() throws IOException { writer.write(""); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); assertTrue(compressedFile.length() > 0); // Should at least contain the btsz value } @@ -70,7 +70,7 @@ void testSingleCharacterCompression() throws IOException { writer.write("aaaa"); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); assertTrue(compressedFile.length() > 0); } @@ -82,7 +82,7 @@ void testRepeatingPatternCompression() throws IOException { writer.write("abcabcabc"); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); assertTrue(compressedFile.length() > 0); @@ -103,7 +103,7 @@ void testLargeRepeatingContent() throws IOException { writer.write(content.toString()); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); // Large repeating content should compress well @@ -122,7 +122,7 @@ void testRandomContent() throws IOException { writer.write(content.toString()); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); } @@ -135,7 +135,7 @@ void testSpecialCharacters() throws IOException { writer.write(content); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); } @@ -151,19 +151,19 @@ void testDictionaryLimitHandling() throws IOException { writer.write(content.toString()); } - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); assertTrue(compressedFile.exists()); } @Test void testStateReset() { // Test that static variables are properly reset - Lzipping.btsz = 42; - Lzipping.big = "test"; + LzwCompressor.bitSize = 42; + LzwCompressor.bitBuffer = "test"; - Lzipping.beginLzipping(inputFile.getAbsolutePath()); + LzwCompressor.beginLzwCompression(inputFile.getAbsolutePath()); - assertEquals(0, Lzipping.btsz); - assertEquals("", Lzipping.big); + assertEquals(0, LzwCompressor.bitSize); + assertEquals("", LzwCompressor.bitBuffer); } } \ No newline at end of file diff --git a/src/test/java/prog/lzw/LunzippingTest.java b/src/test/java/prog/lzw/LzwDecompressorTest.java similarity index 78% rename from src/test/java/prog/lzw/LunzippingTest.java rename to src/test/java/prog/lzw/LzwDecompressorTest.java index 7cd7507..38bdb33 100644 --- a/src/test/java/prog/lzw/LunzippingTest.java +++ b/src/test/java/prog/lzw/LzwDecompressorTest.java @@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*; -class LunzippingTest { +class LzwDecompressorTest { @TempDir Path tempDir; @@ -28,9 +28,9 @@ private File createCompressedFile(String filename, int bitsz1, byte[] content) t @BeforeEach void setUp() { - Lunzipping.big1 = ""; - Lunzipping.bitsz1 = 0; - Lunzipping.pre(); // Initialize the binary to string conversion table + LzwDecompressor.bitBuffer = ""; + LzwDecompressor.bitSize = 0; + LzwDecompressor.initializeByteToStringTable(); // Initialize the binary to string conversion table } @Test @@ -41,7 +41,7 @@ void testBasicDecompression() throws IOException { File compressedFile = createCompressedFile(filename, 8, compressedContent); // Run decompression - Lunzipping.beginLunzipping(compressedFile.getAbsolutePath()); + LzwDecompressor.beginLzwDecompression(compressedFile.getAbsolutePath()); // Check the decompressed file File decompressedFile = new File(tempDir.toFile(), filename); @@ -60,7 +60,7 @@ void testEmptyFile() throws IOException { File compressedFile = createCompressedFile(filename, 8, compressedContent); // Run decompression - Lunzipping.beginLunzipping(compressedFile.getAbsolutePath()); + LzwDecompressor.beginLzwDecompression(compressedFile.getAbsolutePath()); // Check the decompressed file File decompressedFile = new File(tempDir.toFile(), filename); @@ -76,7 +76,7 @@ void testRepeatedPatterns() throws IOException { File compressedFile = createCompressedFile(filename, 8, compressedContent); // Run decompression - Lunzipping.beginLunzipping(compressedFile.getAbsolutePath()); + LzwDecompressor.beginLzwDecompression(compressedFile.getAbsolutePath()); // Check the decompressed file File decompressedFile = new File(tempDir.toFile(), filename); @@ -92,13 +92,13 @@ void testRepeatedPatterns() throws IOException { @Test void testBinaryToStringConversion() { - Lunzipping.pre(); // Initialize conversion table + LzwDecompressor.initializeByteToStringTable(); // Initialize conversion table // Test some known conversions - assertEquals("00000000", Lunzipping.bttost[0], "Conversion for 0"); - assertEquals("00000001", Lunzipping.bttost[1], "Conversion for 1"); - assertEquals("10000000", Lunzipping.bttost[128], "Conversion for 128"); - assertEquals("11111111", Lunzipping.bttost[255], "Conversion for 255"); + assertEquals("00000000", LzwDecompressor.byteToString[0], "Conversion for 0"); + assertEquals("00000001", LzwDecompressor.byteToString[1], "Conversion for 1"); + assertEquals("10000000", LzwDecompressor.byteToString[128], "Conversion for 128"); + assertEquals("11111111", LzwDecompressor.byteToString[255], "Conversion for 255"); } } \ No newline at end of file