2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.modbus.internal;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertThrows;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
23 * @author Sami Salonen - Initial contribution
26 public class AtomicStampedKeyValueTest {
29 public void testInitWithNullValue() {
30 assertThrows(NullPointerException.class, () -> new AtomicStampedValue<>(0, null));
34 public void testGetters() {
35 Object val = new Object();
36 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
37 assertThat(keyValue.getStamp(), is(equalTo(42L)));
38 assertThat(keyValue.getValue(), is(equalTo(val)));
42 public void testUpdateWithSameStamp() {
43 Object val = new Object();
44 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
45 keyValue.update(42L, new Object());
46 assertThat(keyValue.getStamp(), is(equalTo(42L)));
47 assertThat(keyValue.getValue(), is(not(equalTo(val))));
51 public void testUpdateWithDifferentStamp() {
52 Object val = new Object();
53 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
54 keyValue.update(-99L, new Object());
55 assertThat(keyValue.getStamp(), is(equalTo(-99L)));
56 assertThat(keyValue.getValue(), is(not(equalTo(val))));
60 public void testCopy() {
61 Object val = new Object();
62 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
63 AtomicStampedValue<Object> copy = keyValue.copy();
66 assertThat(keyValue.getStamp(), is(equalTo(42L)));
67 assertThat(keyValue.getValue(), is(equalTo(val)));
70 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
71 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
73 // after update they live life of their own
74 Object val2 = new Object();
75 copy.update(-99L, val2);
77 assertThat(keyValue.getStamp(), is(equalTo(42L)));
78 assertThat(keyValue.getValue(), is(equalTo(val)));
80 assertThat(copy.getStamp(), is(equalTo(-99L)));
81 assertThat(copy.getValue(), is(equalTo(val2)));
85 * instance(stamp=x).copyIfStampAfter(x)
88 public void testCopyIfStampAfterEqual() {
89 Object val = new Object();
90 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
91 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(42L);
94 assertThat(keyValue.getStamp(), is(equalTo(42L)));
95 assertThat(keyValue.getValue(), is(equalTo(val)));
98 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
99 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
101 // after update they live life of their own
102 Object val2 = new Object();
103 copy.update(-99L, val2);
105 assertThat(keyValue.getStamp(), is(equalTo(42L)));
106 assertThat(keyValue.getValue(), is(equalTo(val)));
108 assertThat(copy.getStamp(), is(equalTo(-99L)));
109 assertThat(copy.getValue(), is(equalTo(val2)));
113 * instance(stamp=x-1).copyIfStampAfter(x)
116 public void testCopyIfStampAfterTooOld() {
117 Object val = new Object();
118 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
119 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(43L);
121 // keyValue unchanged
122 assertThat(keyValue.getStamp(), is(equalTo(42L)));
123 assertThat(keyValue.getValue(), is(equalTo(val)));
126 assertThat(copy, is(nullValue()));
130 * instance(stamp=x).copyIfStampAfter(x-1)
133 public void testCopyIfStampAfterFresh() {
134 Object val = new Object();
135 AtomicStampedValue<Object> keyValue = new AtomicStampedValue<>(42L, val);
136 AtomicStampedValue<Object> copy = keyValue.copyIfStampAfter(41L);
138 // keyValue unchanged
139 assertThat(keyValue.getStamp(), is(equalTo(42L)));
140 assertThat(keyValue.getValue(), is(equalTo(val)));
143 assertThat(keyValue.getStamp(), is(equalTo(copy.getStamp())));
144 assertThat(keyValue.getValue(), is(equalTo(copy.getValue())));
146 // after update they live life of their own
147 Object val2 = new Object();
148 copy.update(-99L, val2);
150 assertThat(keyValue.getStamp(), is(equalTo(42L)));
151 assertThat(keyValue.getValue(), is(equalTo(val)));
153 assertThat(copy.getStamp(), is(equalTo(-99L)));
154 assertThat(copy.getValue(), is(equalTo(val2)));
158 public void testCompare() {
159 // equal, smaller, larger
160 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(42L, "")),
162 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(41L, ""), new AtomicStampedValue<>(42L, "")),
164 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), new AtomicStampedValue<>(41L, "")),
168 assertThat(AtomicStampedValue.compare(null, new AtomicStampedValue<>(42L, "")), is(equalTo(-1)));
169 assertThat(AtomicStampedValue.compare(new AtomicStampedValue<>(42L, ""), null), is(equalTo(1)));