<commit_before>package simpledb.systemtest;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.junit.Assert;

import simpledb.*;

public class SystemTestUtil {
    public static final TupleDesc SINGLE_INT_DESCRIPTOR =
            new TupleDesc(new Type[]{Type.INT_TYPE});

    private static final int MAX_RAND_VALUE = 1 << 16;

    /** @param columnSpecification Mapping between column index and value. */
    public static HeapFile createRandomHeapFile(
            int columns, int rows, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples)
            throws IOException, DbException, TransactionAbortedException {
        return createRandomHeapFile(columns, rows, MAX_RAND_VALUE, columnSpecification, tuples);
    }

    /** @param columnSpecification Mapping between column index and value. */
    public static HeapFile createRandomHeapFile(
            int columns, int rows, int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples)
            throws IOException, DbException, TransactionAbortedException {
        File temp = createRandomHeapFileUnopened(columns, rows, maxValue,
                columnSpecification, tuples);
        return Utility.openHeapFile(columns, temp);
    }
    
    public static HeapFile createRandomHeapFile(
            int columns, int rows, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples, String colPrefix)
            throws IOException, DbException, TransactionAbortedException {
        return createRandomHeapFile(columns, rows, MAX_RAND_VALUE, columnSpecification, tuples, colPrefix);
    }
    
    public static HeapFile createRandomHeapFile(
            int columns, int rows, int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples, String colPrefix)
            throws IOException, DbException, TransactionAbortedException {
        File temp = createRandomHeapFileUnopened(columns, rows, maxValue,
                columnSpecification, tuples);
        return Utility.openHeapFile(columns, colPrefix, temp);
    }

    public static File createRandomHeapFileUnopened(int columns, int rows,
            int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples) throws IOException {
        if (tuples != null) {
            tuples.clear();
        } else {
            tuples = new ArrayList<ArrayList<Integer>>(rows);
        }

        Random r = new Random();

        // Fill the tuples list with generated values
        for (int i = 0; i < rows; ++i) {
            ArrayList<Integer> tuple = new ArrayList<Integer>(columns);
            for (int j = 0; j < columns; ++j) {
                // Generate random values, or use the column specification
                Integer columnValue = null;
                if (columnSpecification != null) columnValue = columnSpecification.get(j);
                if (columnValue == null) {
                    columnValue = r.nextInt(maxValue);
                }
                tuple.add(columnValue);
            }
            tuples.add(tuple);
        }

        // Convert the tuples list to a heap file and open it
        File temp = File.createTempFile("table", ".dat");
        temp.deleteOnExit();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
        HeapFileEncoder.convert(tuples, out, BufferPool.getPageSize(), columns);
        return temp;
    }

    public static ArrayList<Integer> tupleToList(Tuple tuple) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < tuple.getTupleDesc().numFields(); ++i) {
            int value = ((IntField)tuple.getField(i)).getValue();
            list.add(value);
        }
        return list;
    }

    public static void matchTuples(DbFile f, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        TransactionId tid = new TransactionId();
        matchTuples(f, tid, tuples);
        Database.getBufferPool().transactionComplete(tid);
    }

    public static void matchTuples(DbFile f, TransactionId tid, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        SeqScan scan = new SeqScan(tid, f.getId(), "");
        matchTuples(scan, tuples);
    }

    public static void matchTuples(DbIterator iterator, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        ArrayList<ArrayList<Integer>> copy = new ArrayList<ArrayList<Integer>>(tuples);

        if (Debug.isEnabled()) {
            Debug.log("Expected tuples:");
            for (ArrayList<Integer> t : copy) {
                Debug.log("\t" + Utility.listToString(t));
            }
        }

        iterator.open();<commit_msg>fix: allow some tests to pass with doubles<commit_after>package simpledb.systemtest;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.junit.Assert;

import simpledb.*;

public class SystemTestUtil {
    public static final TupleDesc SINGLE_INT_DESCRIPTOR =
            new TupleDesc(new Type[]{Type.INT_TYPE});

    private static final int MAX_RAND_VALUE = 1 << 16;

    /** @param columnSpecification Mapping between column index and value. */
    public static HeapFile createRandomHeapFile(
            int columns, int rows, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples)
            throws IOException, DbException, TransactionAbortedException {
        return createRandomHeapFile(columns, rows, MAX_RAND_VALUE, columnSpecification, tuples);
    }

    /** @param columnSpecification Mapping between column index and value. */
    public static HeapFile createRandomHeapFile(
            int columns, int rows, int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples)
            throws IOException, DbException, TransactionAbortedException {
        File temp = createRandomHeapFileUnopened(columns, rows, maxValue,
                columnSpecification, tuples);
        return Utility.openHeapFile(columns, temp);
    }
    
    public static HeapFile createRandomHeapFile(
            int columns, int rows, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples, String colPrefix)
            throws IOException, DbException, TransactionAbortedException {
        return createRandomHeapFile(columns, rows, MAX_RAND_VALUE, columnSpecification, tuples, colPrefix);
    }
    
    public static HeapFile createRandomHeapFile(
            int columns, int rows, int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples, String colPrefix)
            throws IOException, DbException, TransactionAbortedException {
        File temp = createRandomHeapFileUnopened(columns, rows, maxValue,
                columnSpecification, tuples);
        return Utility.openHeapFile(columns, colPrefix, temp);
    }

    public static File createRandomHeapFileUnopened(int columns, int rows,
            int maxValue, Map<Integer, Integer> columnSpecification,
            ArrayList<ArrayList<Integer>> tuples) throws IOException {
        if (tuples != null) {
            tuples.clear();
        } else {
            tuples = new ArrayList<ArrayList<Integer>>(rows);
        }

        Random r = new Random();

        // Fill the tuples list with generated values
        for (int i = 0; i < rows; ++i) {
            ArrayList<Integer> tuple = new ArrayList<Integer>(columns);
            for (int j = 0; j < columns; ++j) {
                // Generate random values, or use the column specification
                Integer columnValue = null;
                if (columnSpecification != null) columnValue = columnSpecification.get(j);
                if (columnValue == null) {
                    columnValue = r.nextInt(maxValue);
                }
                tuple.add(columnValue);
            }
            tuples.add(tuple);
        }

        // Convert the tuples list to a heap file and open it
        File temp = File.createTempFile("table", ".dat");
        temp.deleteOnExit();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
        HeapFileEncoder.convert(tuples, out, BufferPool.getPageSize(), columns);
        return temp;
    }

    public static ArrayList<Integer> tupleToList(Tuple tuple) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < tuple.getTupleDesc().numFields(); ++i) {
            Field field = tuple.getField(i);
            int value;
            if (field instanceof IntField) {
            	value = ((IntField)field).getValue();
            } else if (field instanceof DoubleField) {
            	value = (int) ((DoubleField)field).getValue();
            } else {
            	throw new RuntimeException("Unexpected field type.");
            }
            list.add(value);
        }
        return list;
    }

    public static void matchTuples(DbFile f, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        TransactionId tid = new TransactionId();
        matchTuples(f, tid, tuples);
        Database.getBufferPool().transactionComplete(tid);
    }

    public static void matchTuples(DbFile f, TransactionId tid, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        SeqScan scan = new SeqScan(tid, f.getId(), "");
        matchTuples(scan, tuples);
    }

    public static void matchTuples(DbIterator iterator, List<ArrayList<Integer>> tuples)
            throws DbException, TransactionAbortedException, IOException {
        ArrayList<ArrayList<Integer>> copy = new ArrayList<ArrayList<Integer>>(tuples);

        if (Debug.isEnabled()) {
            Debug.log("Expected tuples:");
            for (ArrayList<Integer> t : copy) {
                Debug.log("\t" + Utility.listToString(t));
            }
        }

        iterator.open();
{'fraction_non_alphanumeric': 0.09168865435356201, 'fraction_numerical': 0.0011307953260459858, 'mean_word_length': 2.691478260869565, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7553834', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2660, 'n_words': 744}