package com.alibaba.fastjson2.v1issues.issue_1400;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue1424 {
    public static class IntegerVal {
        private int v;

        public void setV(int v) {
            this.v = v;
        }

        @Override
        public String toString() {
            return String.valueOf(v);
        }
    }

    public static class FloatVal {
        private float v;

        public void setV(float v) {
            this.v = v;
        }

        @Override
        public String toString() {
            return String.valueOf(v);
        }
    }

    @Test
    public void test_for_issue_int() {
        Map<String, Long> intOverflowMap = new HashMap<String, Long>();
        long intOverflow = Integer.MAX_VALUE;
        intOverflowMap.put("v", intOverflow + 1);
        String sIntOverflow = JSON.toJSONString(intOverflowMap);

        Exception error = null;
        try {
            JSON.parseObject(sIntOverflow, IntegerVal.class);
        } catch (Exception e) {
            error = e;
        }
        assertNotNull(error);
    }

    @Test
    public void test_for_issue_float() {
        Map<String, Double> floatOverflowMap = new HashMap<String, Double>();
        double floatOverflow = Float.MAX_VALUE;
        floatOverflowMap.put("v", floatOverflow + 1);
        String sFloatOverflow = JSON.toJSONString(floatOverflowMap);

        assertEquals("{\"v\":3.4028234663852886E38}", sFloatOverflow);
        FloatVal floatVal = JSON.parseObject(sFloatOverflow, FloatVal.class);
        assertEquals(3.4028235E38F, floatVal.v);

        assertEquals(floatVal.v, Float.parseFloat("3.4028234663852886E38"));
    }

    @Test
    public void test_for_issue_float_infinity() {
        Map<String, Double> floatOverflowMap = new HashMap<String, Double>();
        double floatOverflow = Float.MAX_VALUE;
        floatOverflowMap.put("v", floatOverflow + floatOverflow);
        String sFloatOverflow = JSON.toJSONString(floatOverflowMap);

        System.out.println(sFloatOverflow);
        assertEquals("{\"v\":6.805646932770577E38}", sFloatOverflow);
        FloatVal floatVal = JSON.parseObject(sFloatOverflow, FloatVal.class);
        assertEquals(Float.parseFloat("6.805646932770577E38"), floatVal.v);
    }
}
