<commit_before>package org.exist.xquery.value.test;

import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.*;

/**
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class DurationTest extends AbstractTimeRelatedTestCase {
	public void testStringFormat1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		assertEquals("P1Y2M3DT1H2M3S", dv.getStringValue());
	}
	public void testStringFormat2() throws XPathException {
		DurationValue dv = new DurationValue("P1Y13M1DT25H65M66.5S");
		assertEquals("P2Y1M2DT2H6M6.5S", dv.getStringValue());
	}
	public void testStringFormat3() throws XPathException {
		DurationValue dv = new DurationValue("P0Y");
		assertEquals("PT0S", dv.getStringValue());
	}
	public void testNegate() throws XPathException {
		DurationValue dv = new DurationValue("P2D");
		dv = dv.negate();
		assertEquals("-P2D", dv.getStringValue());
		assertEquals(-2, dv.getPart(DurationValue.DAY));
	}
	public void testConvert1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		YearMonthDurationValue ymdv = (YearMonthDurationValue) dv.convertTo(Type.YEAR_MONTH_DURATION);
		assertEquals("P1Y2M", ymdv.getStringValue());
	}
	public void testConvert2() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		DayTimeDurationValue dtdv = (DayTimeDurationValue) dv.convertTo(Type.DAY_TIME_DURATION);
		assertEquals("P3DT1H2M3S", dtdv.getStringValue());
	}
	public void testConvert3() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3.5S");
		DayTimeDurationValue dtdv = (DayTimeDurationValue) dv.convertTo(Type.DAY_TIME_DURATION);
		assertEquals("P3DT1H2M3.5S", dtdv.getStringValue());
	}
	public void testGetPart1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
		assertEquals(1, dv.getPart(DurationValue.YEAR));
		assertEquals(2, dv.getPart(DurationValue.MONTH));
		assertEquals(3, dv.getPart(DurationValue.DAY));
		assertEquals(4, dv.getPart(DurationValue.HOUR));
		assertEquals(5, dv.getPart(DurationValue.MINUTE));
		assertEquals(6, dv.getSeconds(), 0);
	}
	public void testGetPart2() throws XPathException {
		DurationValue dv = new DurationValue("-P1Y2M3DT4H5M6S");
		assertEquals(-1, dv.getPart(DurationValue.YEAR));
		assertEquals(-2, dv.getPart(DurationValue.MONTH));
		assertEquals(-3, dv.getPart(DurationValue.DAY));
		assertEquals(-4, dv.getPart(DurationValue.HOUR));
		assertEquals(-5, dv.getPart(DurationValue.MINUTE));
		assertEquals(-6, dv.getSeconds(), 0);
	}
	public void testGetType() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
		assertEquals(Type.DURATION, dv.getType());
	}
	
	//Removed : what should this comparison fail ? -pb
	/*
	public void testCompareFail1() throws XPathException {
		try {
			DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
			dv.compareTo(null, dv);
			fail();
		} catch (XPathException e) {
			// expected
		}
	}
	*/
	
	public void testCompareFail2() throws XPathException {
		try {
			DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
			dv.compareTo(null, Constants.EQ, dv);
			fail();
		} catch (XPathException e) {
			// expected
		}
	}
	public void testCompareFail3() throws XPathException {
		try {
			DurationValue dv1 = new DurationValue("P1Y2M3DT4H5M6S"), dv2 = new DayTimeDurationValue("P1D");<commit_msg>Removed arithmetics operations on xs:duration that have been improperly allowed. Added *some* comparison operations on xs:duration<commit_after>package org.exist.xquery.value.test;

import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.*;

/**
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class DurationTest extends AbstractTimeRelatedTestCase {
	public void testStringFormat1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		assertEquals("P1Y2M3DT1H2M3S", dv.getStringValue());
	}
	public void testStringFormat2() throws XPathException {
		DurationValue dv = new DurationValue("P1Y13M1DT25H65M66.5S");
		assertEquals("P2Y1M2DT2H6M6.5S", dv.getStringValue());
	}
	public void testStringFormat3() throws XPathException {
		DurationValue dv = new DurationValue("P0Y");
		assertEquals("PT0S", dv.getStringValue());
	}
	public void testNegate() throws XPathException {
		DurationValue dv = new DurationValue("P2D");
		dv = dv.negate();
		assertEquals("-P2D", dv.getStringValue());
		assertEquals(-2, dv.getPart(DurationValue.DAY));
	}
	public void testConvert1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		YearMonthDurationValue ymdv = (YearMonthDurationValue) dv.convertTo(Type.YEAR_MONTH_DURATION);
		assertEquals("P1Y2M", ymdv.getStringValue());
	}
	public void testConvert2() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3S");
		DayTimeDurationValue dtdv = (DayTimeDurationValue) dv.convertTo(Type.DAY_TIME_DURATION);
		assertEquals("P3DT1H2M3S", dtdv.getStringValue());
	}
	public void testConvert3() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT1H2M3.5S");
		DayTimeDurationValue dtdv = (DayTimeDurationValue) dv.convertTo(Type.DAY_TIME_DURATION);
		assertEquals("P3DT1H2M3.5S", dtdv.getStringValue());
	}
	public void testGetPart1() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
		assertEquals(1, dv.getPart(DurationValue.YEAR));
		assertEquals(2, dv.getPart(DurationValue.MONTH));
		assertEquals(3, dv.getPart(DurationValue.DAY));
		assertEquals(4, dv.getPart(DurationValue.HOUR));
		assertEquals(5, dv.getPart(DurationValue.MINUTE));
		assertEquals(6, dv.getSeconds(), 0);
	}
	public void testGetPart2() throws XPathException {
		DurationValue dv = new DurationValue("-P1Y2M3DT4H5M6S");
		assertEquals(-1, dv.getPart(DurationValue.YEAR));
		assertEquals(-2, dv.getPart(DurationValue.MONTH));
		assertEquals(-3, dv.getPart(DurationValue.DAY));
		assertEquals(-4, dv.getPart(DurationValue.HOUR));
		assertEquals(-5, dv.getPart(DurationValue.MINUTE));
		assertEquals(-6, dv.getSeconds(), 0);
	}
	public void testGetType() throws XPathException {
		DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
		assertEquals(Type.DURATION, dv.getType());
	}
	
	public void testCompareSucceeds() throws XPathException {
		try {
			DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
			//eq and ne comparison operators are allowed
			dv.compareTo(null, Constants.EQ, dv);			
		} catch (XPathException e) {
			fail();
		}
	}
	
	public void testCompareFail1() throws XPathException {
		try {
			DurationValue dv = new DurationValue("P1Y2M3DT4H5M6S");
			dv.compareTo(null, Constants.LT, dv);
			fail();
		} catch (XPathException e) {
			// expected
		}
	}	

	public void testCompareFail3() throws XPathException {
		try {
			DurationValue dv1 = new DurationValue("P1Y2M3DT4H5M6S"), dv2 = new DayTimeDurationValue("P1D");
{'fraction_non_alphanumeric': 0.12542857142857142, 'fraction_numerical': 0.03671428571428571, 'mean_word_length': 5.783914728682171, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6713645', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2392, 'n_words': 452}