#include "gtest/gtest.h"
#include "../src/lru_cache.cu"
//#include "cache.h"
#include <cstdint>
#include <cuda_runtime.h>
#include <unordered_set>
#include <vector>
#include <numeric>
#include <random>
#include <algorithm>

namespace gpucache{

    void TestCache(Cache<uint32_t ,uint32_t>& cache, uint32_t num_elem_per_value){

        std::unordered_set<uint32_t> in_cache;
        const size_t n_iter = 32;
        const uint32_t n_keys = 1024;


        uint32_t* d_keys;
        uint32_t* keys;
        uint32_t* d_values;
        uint32_t* values;
        uint32_t* d_n_missing;
        uint32_t* n_missing;
        uint32_t* d_missing_keys;
        uint32_t* missing_keys;
        uint32_t* d_n_evict;
        uint32_t* n_evict;
        uint32_t* d_evict_keys;
        uint32_t* evict_keys;
        bool* find_mask;
        bool* d_find_mask;

        const size_t keys_size = n_keys * sizeof(uint32_t);

        const size_t values_size = n_keys * num_elem_per_value * sizeof(uint32_t);

        const size_t mask_size = n_keys * sizeof(bool);


        CHECK(cudaMalloc(&d_keys,keys_size));
        CHECK(cudaMalloc(&d_values, values_size));
        CHECK(cudaMalloc(&d_n_missing,sizeof(uint32_t)));
        CHECK(cudaMalloc(&d_find_mask, mask_size));
        CHECK(cudaMalloc(&d_missing_keys,keys_size));
        CHECK(cudaMalloc(&d_evict_keys,keys_size));
        CHECK(cudaMalloc(&d_n_evict,sizeof(uint32_t)));

        CHECK(cudaMallocHost(&keys,keys_size));
        CHECK(cudaMallocHost(&values,values_size));
        CHECK(cudaMallocHost(&n_missing,sizeof(uint32_t)));
        CHECK(cudaMallocHost(&find_mask,mask_size));

        CHECK(cudaMallocHost(&missing_keys,keys_size));

        CHECK(cudaMallocHost(&evict_keys,keys_size));
        CHECK(cudaMallocHost(&n_evict,sizeof(uint32_t)));




        std::vector<uint32_t> random_keys(n_keys * n_iter);
        std::iota(random_keys.begin(),random_keys.end(),1);
        std::random_device rd;
        std::mt19937 g(rd());

        for (size_t iter = 0; iter < n_iter; iter++){

            std::cout << "iter " << iter << std::endl;
            uint32_t expect_n_missing = 0;
            CHECK(cudaMemset(d_n_missing,0,sizeof(uint32_t)));
            CHECK(cudaMemset(d_find_mask,0,mask_size));
            CHECK(cudaMemset(d_n_evict,0,sizeof(uint32_t)));
            // CHECK(cudaMemset(d_missing_keys,0,keys_size));
            // CHECK(cudaMemset(d_evict_keys, 0, keys_size));


            std::shuffle(random_keys.begin(), random_keys.end(),g);
            std::copy(random_keys.begin(),random_keys.begin()+n_keys,keys);
            // for (auto i = keys; i < keys + n_keys; i++){
            //     std::cout << *i << " ";
            // }
            // std::cout << std::endl;

            std::unordered_set<uint32_t> expect_missing_keys_set;
            std::unordered_set<uint32_t> keys_set; // store current iter keys
            for (size_t i = 0; i < n_keys; ++i) {
                keys_set.emplace(keys[i]);
                if (in_cache.count(keys[i]) == 0) {
                    expect_missing_keys_set.emplace(keys[i]);
                    expect_n_missing += 1;
                }
            }

            // test get

            std::cout << "test get" << std::endl;
            CHECK(cudaMemcpy(d_keys,keys,keys_size,cudaMemcpyHostToDevice));
            cudaStream_t stream;
            CHECK(cudaStreamCreate(&stream));
            dim3 cblock(256);
            dim3 cgrid(((cache.Capacity() + warpsize - 1) / warpsize + cblock.x - 1) / cblock.x);
            // std::cout << "check locks..............." << std::endl;
            // checkLocks<<<cgrid, cblock>>>((cache.Capacity() + warpsize - 1) / warpsize, cache.Mutex());
            // CHECK(cudaDeviceSynchronize());
            cache.Get(&stream,n_keys,d_keys,d_values,d_find_mask);
            CHECK(cudaDeviceSynchronize());
            CHECK(cudaMemcpy(find_mask,d_find_mask,mask_size,cudaMemcpyDeviceToHost));

            dim3 block(256);
            dim3 grid((n_keys + block.x - 1)/block.x);
            // CollectMissingKeys<uint32_t><<<grid,block>>>(n_keys,d_keys,d_find_mask,d_n_missing,d_missing_keys);
            CollectMissingKeysNew<uint32_t><<<grid,block>>>(n_keys,d_keys,d_find_mask,d_n_missing,d_missing_keys);
            CHECK(cudaDeviceSynchronize());
            CHECK(cudaMemcpy(n_missing,d_n_missing, sizeof(uint32_t),cudaMemcpyDefault));
            CHECK(cudaMemcpy(missing_keys,d_missing_keys,keys_size,cudaMemcpyDefault));
            ASSERT_EQ(expect_n_missing, *n_missing) << "expect_n_missing is " << expect_n_missing << " n_missing is " << *n_missing;
            std::unordered_set<uint32_t> missing_keys_set(missing_keys, missing_keys + *n_missing);
            ASSERT_EQ(missing_keys_set,expect_missing_keys_set);
            // check get value 
            CHECK(cudaMemcpy(values,d_values,values_size,cudaMemcpyDefault));
            for (size_t i = 0; i < n_keys ; i += 1) {
                if(find_mask[i]){
                    ASSERT_EQ(keys[i] + 123,values[i * num_elem_per_value]) << "key[" << i << "] = " << keys[i] <<  " doesn't get correct value should be " << keys[i] + 123 << " get " << values[i * num_elem_per_value];
                }
                
            }


            //test put
            std::cout << "test put" << std::endl;
            memset(values, 0,values_size);
            for (size_t i = 0; i < n_keys; i++){
                values[i * num_elem_per_value] = keys[i] + 123;
            }
            CHECK(cudaMemcpy(d_values,values,values_size,cudaMemcpyDefault));
            cache.Put(&stream,n_keys,d_keys,d_values,d_n_evict,d_evict_keys);
            CHECK(cudaDeviceSynchronize());
            CHECK(cudaMemcpy(n_evict,d_n_evict,sizeof(uint32_t),cudaMemcpyDefault));
            CHECK(cudaMemcpy(evict_keys,d_evict_keys,*n_evict * sizeof(uint32_t), cudaMemcpyDefault));

            std::unordered_set<uint32_t> evict_keys_set(evict_keys, evict_keys + *n_evict);
            CHECK(cudaDeviceSynchronize());
            std::cout << "check evict keys in cache or in current insert batch, n_evict = " << *n_evict << std::endl;
            for(size_t i = 0; i < *n_evict; i++){
                ASSERT_TRUE(in_cache.count(evict_keys[i]) > 0 || keys_set.count(evict_keys[i]) > 0) << "evict_keys[" << i << "] = " << evict_keys[i];
            }
            for (size_t i = 0; i < n_keys; ++i){in_cache.emplace(keys[i]);}
            for (size_t i = 0; i < *n_evict; ++i){in_cache.erase(evict_keys[i]);}

            // std::cout << "check locks..............." << std::endl;
            // checkLocks<<<cgrid, cblock>>>((cache.Capacity() + warpsize - 1) / warpsize, cache.Mutex());
            // CHECK(cudaDeviceSynchronize());
            printf("---------------------------------------------------\n");

        }
        CHECK(cudaFree(d_keys));
        CHECK(cudaFree(d_values));
        CHECK(cudaFree(d_n_missing));
        CHECK(cudaFree(d_find_mask));
        CHECK(cudaFree(d_missing_keys));
        CHECK(cudaFree(d_evict_keys));
        CHECK(cudaFree(d_n_evict));
        CHECK(cudaFreeHost(keys));
        CHECK(cudaFreeHost(values));
        CHECK(cudaFreeHost(n_missing));
        CHECK(cudaFreeHost(find_mask));
        CHECK(cudaFreeHost(missing_keys));
        CHECK(cudaFreeHost(evict_keys));
        CHECK(cudaFreeHost(n_evict));


    }

    TEST(GPUCACHE,LRUCACHE){
        CacheConfig cfg{};
        cfg.strategy = CacheConfig::CacheEvictStrategy::LRU;

        cfg.valueSize = 32;
        cfg.capacity = 4096 * 2;
        cfg.keySize = 4;
        cfg.maxQueryNum = 2048;

        LRUCache<uint32_t,uint32_t> cache(cfg);
        TestCache(cache,cfg.valueSize / sizeof(uint32_t));
    }
}

