#include <iostream>
#include <cstdlib>
#include <sys/shm.h>
#include "shm_keys.hpp"
#include "row.hpp"
#include "sort_algorithms.hpp"

using namespace std;

// Tiny sanity check: attach to the shared random vector, wrap the first
// `n` bytes in a row<uint8_t>, sort it in place, and print before/after.
int main(int argc, char** argv) {
    int n = (argc >= 2) ? atoi(argv[1]) : 20;

    int shmid = shmget(SHM_KEY_ORIGINAL, 1, 0666);
    if(shmid == -1) {
        cerr << "Error: Run shm_generator first!" << endl;
        return 1;
    }
    uint8_t* base = (uint8_t*)shmat(shmid, NULL, 0);

    row<uint8_t> test_row(base, static_cast<uint32_t>(n), false);

    cout << "SHM data before: " << test_row << endl;
    quick_sort(test_row);
    cout << "SHM data after sort: " << test_row << endl;

    shmdt(base);
    return 0;
}
