package com.atlassian.bitbucket.mesh.util;

import org.junit.Test;

import static com.atlassian.bitbucket.mesh.util.ShaUtils.compare;
import static org.junit.Assert.*;

public class ShaUtilsTest {

    @Test
    public void testCompare() {
        // same length
        assertEquals(0, compare("aabbcc", "aabbcc"));
        assertEquals(0, compare("ccbbaa", "ccbbaa"));
        assertTrue(compare("aabbcc", "aabbdd") < 0);
        assertTrue(compare("aabbee", "aabbdd") > 0);

        // different length
        assertEquals(0, compare("aabbcc", "aa"));
        assertEquals(0, compare("aabb", "aabbcc"));
        assertTrue(compare("aabbcc", "aabbddefghij") < 0);
        assertTrue(compare("aabbee12345", "aabbdd") > 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCompareEmptyHash() {
        compare("", "aabbccddee");
    }

    @Test
    public void testHashesMatchNulls() {
        assertTrue(ShaUtils.hashesMatch(null, null));
        assertFalse(ShaUtils.hashesMatch("aabbcc", null));
        assertFalse(ShaUtils.hashesMatch(null, "aabbcc"));
    }

    @Test
    public void testHashesMatchPrefix() {
        //prefix match
        assertTrue(ShaUtils.hashesMatch("aabbcc", "aabbccdd"));
        assertTrue(ShaUtils.hashesMatch("aabbccdd", "aabbcc"));
        //prefix mismatch
        assertFalse(ShaUtils.hashesMatch("aabbccdd", "aabcc"));
        assertFalse(ShaUtils.hashesMatch("aabcc", "aabbcc"));
        //exact match
        assertTrue(ShaUtils.hashesMatch("aabbcc", "aabbcc"));
    }
}