<commit_before>/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/adc/adc_emul.h>

#include "charger.h"
#include "charge_state.h"
#include "charger_profile_override.h"
#include "common.h"
#include "config.h"
#include "hooks.h"
#include "util.h"
#include "temp_sensor.h"
#include "temp_sensor/temp_sensor.h"

#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
#define CHARGER_TEMP TEMP_SENSOR_ID(DT_NODELABEL(temp_charger))
#define ORIGINAL_CURRENT 5000
#define TEMP_THRESHOLD 55

struct charge_state_data curr;
static int fake_voltage;
int count;

/* Limit charging current table : 3600/3000/2400/1800
 * note this should be in descending order.
 */
static uint16_t current_table[] = {
	3600,
	3000,
	2400,
	1800,
};

int setup_faketemp(int fake_voltage)
{
	const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
	int emul_temp;

	emul_temp = adc_emul_const_value_set(
		adc_dev, temp_sensors[CHARGER_TEMP].idx, fake_voltage);
	return emul_temp;
}

static void ignore_first_minute(void)
{
	for (int i = 0; i < 60; i++) {
		hook_notify(HOOK_SECOND);
	}
}

ZTEST(temp_tentacruel, test_decrease_current)
{
	fake_voltage = 376;
	curr.batt.flags |= BATT_FLAG_RESPONSIVE;
	count = 0;

	setup_faketemp(fake_voltage);
	/* Calculate per minute temperature.
	 * It's expected low temperature when the first 60 seconds.
	 */
	ignore_first_minute();
	for (int i = 1; i < 26; i++) {
		hook_notify(HOOK_SECOND);
		curr.requested_current = ORIGINAL_CURRENT;
		charger_profile_override(&curr);
		if (i % 6 == 0) {
			zassert_equal(current_table[count],
				      curr.requested_current, NULL);
			count++;
		}<commit_msg>zephyr: fix incorrect adc channel value in krabby test<commit_after>/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/adc/adc_emul.h>

#include "charger.h"
#include "charge_state.h"
#include "charger_profile_override.h"
#include "common.h"
#include "config.h"
#include "hooks.h"
#include "util.h"
#include "temp_sensor.h"
#include "temp_sensor/temp_sensor.h"

#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
#define CHARGER_TEMP TEMP_SENSOR_ID(DT_NODELABEL(temp_charger))
#define ORIGINAL_CURRENT 5000
#define TEMP_THRESHOLD 55

struct charge_state_data curr;
static int fake_voltage;
int count;

/* Limit charging current table : 3600/3000/2400/1800
 * note this should be in descending order.
 */
static uint16_t current_table[] = {
	3600,
	3000,
	2400,
	1800,
};

int setup_faketemp(int fake_voltage)
{
	const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
	const uint8_t channel_id =
		DT_IO_CHANNELS_INPUT(DT_NODELABEL(adc_charger));
	int emul_temp;

	emul_temp = adc_emul_const_value_set(adc_dev, channel_id, fake_voltage);
	return emul_temp;
}

static void ignore_first_minute(void)
{
	for (int i = 0; i < 60; i++) {
		hook_notify(HOOK_SECOND);
	}
}

ZTEST(temp_tentacruel, test_decrease_current)
{
	fake_voltage = 376;
	curr.batt.flags |= BATT_FLAG_RESPONSIVE;
	count = 0;

	setup_faketemp(fake_voltage);
	/* Calculate per minute temperature.
	 * It's expected low temperature when the first 60 seconds.
	 */
	ignore_first_minute();
	for (int i = 1; i < 26; i++) {
		hook_notify(HOOK_SECOND);
		curr.requested_current = ORIGINAL_CURRENT;
		charger_profile_override(&curr);
		if (i % 6 == 0) {
			zassert_equal(current_table[count],
				      curr.requested_current, NULL);
			count++;
		}
{'fraction_non_alphanumeric': 0.11114093959731544, 'fraction_numerical': 0.031946308724832215, 'mean_word_length': 5.078303425774878, 'pattern_counts': {'":': 0, '<': 15, '<?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': '6633839', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1429, 'n_words': 331}